Can __iter__ be used as a classmethod?

Michele Simionato mis6 at pitt.edu
Tue Mar 4 17:11:32 EST 2003


"Giovanni Bajo" <noway at sorry.com> wrote in message news:<8g69a.176850$YG2.5353209 at twister1.libero.it>...
> <snip>
> I still have a small doubt by classmethod(). Let's consider this:
> 
> class A:
>     def f(cls):
>         pass
>     f = classmethod(f)
> 
>     def g(self):
>         pass
> 
> a = A()
> 
> Now, in my current understanding, the differences between A.f and A.g are:
> 
> 1) A.f() is a valid call (and the function will receive class A as
> parameter), while A.g() will raise a runtime exception (unbound method).
> A.g(a) is correct because it is equivalent to a.g().
> 2) a.f() is a valid call (and the function will receive class A, aka
> type(a), as parameter); a.g() is a valid call as well, and the function will
> receive a as parameter
> 
> Also, both are attributes of A and not of a. They can be used on a because
> they are automatically looked up on type(a), but they belong to A.
> Another source of confusion in my mind is that type(A.g) is called "instance
> method", because it must be bound to an instance of the class, to become
> 'callable'. But it does not 'belong' to the instance, it is actually defined
> in A.__dict__, not in A().__dict__.
> 
> <snip>
> By the> 
> way, it seems that the word 'inheritance' has been abused in this context:
> this is not the normal inheritance as in class A(B), but something different
> (A() will lookup attributes from A if they have not been rebound, and A will
> lookup attributes from A.__metaclass__ -- more generically, A will lookup
> attributes in type(A)). Is there a more correct word for this?
> 'meta-inheritance'? :)
> 
> Thanks again for your time
> 
> Giovanni Bajo

I think you have got all right. It is true that the current terminogy is
misleading at times :-(


                                          Michele




More information about the Python-list mailing list