__subclasses__

Michael Hudson mwh at python.net
Fri Jun 14 12:09:31 CEST 2002


jimrowe at optilink.com (James Rowe) writes:

> A search for __subclasses__ on the www.python.org
> documentation search pages turns up empty. I can
> find not mention of it in my Python books or even
> in the "What's new in Python 2.2" document.  But:
> 
> 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.

> But there is a __subclasses__ method:
> 
> Foo.__subclasses__() # returns:
> 
>  [<class '__main__.Bar'>]
> 
> 
> So I suppose the question is: Why is __subclasses__ not documented
> or listed?

Dunno.  Lack of time would be my guess.  Lots of this stuff isn't
documented.

> Can we safely use __subclasses__ and not worry about it "going away"
> in the future?

Well the implementation needs to have access to the functionality it
provides (I think this is for situtations like:

class a(object):
 pass
class b(a):
 pass

a.__getitem__ = some_meth

b()[1] should now invoke some_meth

but I'm not sure).  This doesn't mean that the current exposure of the
functionality to Python code is not going to disappear or change in
spelling, but I'd say it was pretty unlikely.

Cheers,
M.

-- 
  It's actually a corruption of "starling".  They used to be carried.
  Since they weighed a full pound (hence the name), they had to be
  carried by two starlings in tandem, with a line between them.
                 -- Alan J Rosenthal explains "Pounds Sterling" on asr



More information about the Python-list mailing list