__subclasses__

Walter Moreira walterm at parque.homelinux.net
Tue Jun 18 09:32:06 EDT 2002


On Fri, Jun 14, 2002 at 10:09:31AM +0000, Michael Hudson wrote:
> > class Foo(object):
> >     def __init__(self):
> >         object.__init__(self)
> > 
> > class Bar(Foo):
> > 	pass
> > 
> > dir(Foo) # returns:
> > 
> >  ['__class__', '__delattr__', '__dict__', '__doc__',
> >  '__getattribute__', '__hash__', '__init__', '__module__',
> >  '__new__', '__reduce__', '__repr__', '__setattr__',
> >  '__str__', '__weakref__']
> 
> Whether things show up in dir() is a bit of a lottery (think about
> __getattr__ methods!).  Notice __bases__ isn't in there either, for
> example.

The __bases__ and __subclasses__ (and others) are listed in the 'type'
object:

    >>> class Foo:
    ...     pass
    ...
    >>> dir(type(type(Foo)))
    ['__base__', '__bases__', '__basicsize__', '__call__', '__class__',
    '__cmp__', '__delattr__', '__dict__', '__dictoffset__', '__doc__',
    '__flags__', '__getattribute__', '__hash__', '__init__', '__itemsize__',
    '__module__', '__mro__', '__name__', '__new__', '__reduce__',
    '__repr__', '__setattr__', '__str__', '__subclasses__',
    '__weakrefoffset__', 'mro']

Walter





More information about the Python-list mailing list