Can __iter__ be used as a classmethod?

Thomas Heller theller at python.net
Tue Mar 11 09:22:31 EST 2003


"Greg Ewing (using news.cis.dfn.de)" <me at privacy.net> writes:

> Alex Martelli wrote:
> > Aren't these reasons enough?  How else would you code a method that
> > you can indifferently call either on any instance or on the class
> > object itself?
> 
> How often do people actually *need* that functionality,
> though? Smalltalkers don't seem to be bothered by the
> need to know whether to call a method on a class or an
> instance. Can someone provide a compelling use case
> for this in Python?

Although I use metaclasses and class-methods extensively in the ctypes
module, I haven't yet needed to call a class-method from an instance.

Maybe 'classmethod' is as weird as what some people aks for in this
forum (and they are overly happy if I point out that it is indeed
possible, while the 'right' answer would probably be 'you don't want to
desing your program in this way'): Have a method that binds to the class
if called from the class, and binds to the instance if called from an
instance.

> 
> > And _having_ to write a custom metaclass as the
> > only way to get classmethods would be somewhat of an overkill.

While Guido doesn't like this notation, I actually find it nice:

class MyClass(object):

    class __metaclass__(type):

        def my_class_methods(cls):
            .....

    def my_normal_method(self):
        ....

(Donald Beaudry had a similar notation in his objectmodule.c, which
also provided class methods).

Thomas




More information about the Python-list mailing list