Can __iter__ be used as a classmethod?

Michele Simionato mis6 at pitt.edu
Fri Mar 7 11:24:28 EST 2003


"Samuele Pedroni" <pedronis at bluewin.ch> wrote in message news:<3e67cc71_2 at news.bluewin.ch>...
> 
> Although it is an underdocumented feature, e.g. the pure python version of
> super here
> 
> http://www.python.org/2.2.2/descrintro.html#cooperation
> 
> would not work. Here's how the feature was added:
> 
> www.python.org/sf/535444
> 
> and for a normal method:
> 
> >>> class C(object):
> ...  def f(self): pass
> ...
> >>> class D(C): pass
>  ...
> >>> D.f
>  <unbound method D.f>
> >>> super(D,D).f
> <bound method D.f of <class '__main__.D'>>
> 
> super(D,D) appear to misbehave.

Another minor inconsistency (IMHO) is that "metamethods" i.e. regular
methods in the metaclass are not retrieved by "dir":

>>> class M(type): 
...      def mm(cls):
...          "This is a metamethod"
...          return cls
...
class C(object):
...   __metaclass__=M
...   def pm(self):
...          "This is plain method"
...          return self
...
>>> dir(C) #retrieves the plain method 'pm', not the metamethod 'mm'
['__class__', '__delattr__', '__dict__', '__doc__',
'__getattribute__', '__hash__', '__init__', '__metaclass__',
'__module__', '__new__', '__reduce__', '__repr__', '__setattr__',
'__str__', '__weakref__', 'pm']

In order to retrieve 'mm' I need to invoke dir(M): this is slightly
inconsistent because usually when 'dir' is invoked on an instance, it
automatically retrieves the methods of its class, therefore I would
expect that invoked on a class it would retrieve the methods of its
metaclass. On the other hand, this could be done by design, in order
to avoid cluttering of the output of 'dir'. 'dir' is not meant to
retrieve everything. It is also a documented behaviour:

>>> print dir.__doc__
dir([object]) -> list of strings

Return an alphabetized list of names comprising (some of) the
attributes
of the given object, and of attributes reachable from it:

No argument:  the names in the current scope.
Module object:  the module attributes.
Type or class object:  its attributes, and recursively the attributes
of
    its bases.
Otherwise:  its attributes, its class's attributes, and recursively
the
    attributes of its class's base classes.

                                         Michele




More information about the Python-list mailing list