class method question

Fredrik Lundh fredrik at pythonware.com
Wed Oct 25 11:24:16 EDT 2006


Sylvain Ferriol wrote:

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

You're trying to put two objects with the same name in the same namespace.

> 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:

Python works better if you use it to write Python programs.

</F>




More information about the Python-list mailing list