class method question

Sylvain Ferriol sferriol at imag.fr
Wed Oct 25 10:55:04 EDT 2006


hello
can you explain why python does not see difference between instance 
method and class method, having the same name

example
 >>> class toto(object):
...     def f(self):
...             print('instance method')
...     @classmethod
...     def f(cls):
...             print('class method')
...
 >>> t=toto()
 >>> t.f
<bound method type.f of <class '__main__.toto'>>
 >>> t.f()
class method
 >>> toto.f()
class method


if i do the same in ruby:
"""
class Toto
   def meth
     print "instance method\n"
   end

   def Toto.meth
     print "class method\n"
   end
end

i = Toto.new
i.meth
Toto.meth
"""

$> ruby1.9 test_classmethod.rb
instance method
class method

thanks
sylvain



More information about the Python-list mailing list