Friday, 4 March 2011

Static methods or class methods in Ruby and Python

Today when learning how to define a class method in Ruby I was curious how would I do that in Python. Here I have demonstrated how static/class methods look in these languages.


Ruby


class Greeter
def self.say_hi name
puts "Hi #{name}."
end
end
=begin
Output:
irb(main):007:0> Greeter.say_hi 'Ruby'
Hi Ruby.
=end
view raw gistfile1.rb hosted with ❤ by GitHub


Python


class Greeter:
@staticmethod
def say_hi(name):
print ('Hi %s.') % name
#Output:
# Greeter.say_hi('Python')
# >> Hi Python.
view raw gistfile1.py hosted with ❤ by GitHub

No comments: