Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

__subclasses__

0 views
Skip to first unread message

James Rowe

unread,
Jun 13, 2002, 1:57:47 PM6/13/02
to
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__']

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? Can we safely use __subclasses__ and not worry about
it "going away" in the future?


Michael Hudson

unread,
Jun 14, 2002, 6:09:31 AM6/14/02
to
jim...@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

Johannes Gijsbers

unread,
Jun 18, 2002, 4:53:01 AM6/18/02
to
jim...@optilink.com (James Rowe) wrote in message news:<a9bcagho74....@optilink.com>...

> So I suppose the question is: Why is __subclasses__ not documented
> or listed? Can we safely use __subclasses__ and not worry about
> it "going away" in the future?

Last time I checked most of the work done in unifying types and
classes wasn't documented yet. I'm not sure __subclasses__ and the
unification are related, but I guess so.

The only info I've found was a thread by Bruce Eckel[1]. He also
mentions it in the draft of his book Thinking in Python.

jlg

[1] http://mail.python.org/pipermail/python-list/2001-December/075864.html
[2] http://www.mindview.net/Books/TIPython

Walter Moreira

unread,
Jun 18, 2002, 9:32:06 AM6/18/02
to
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


Oren Tirosh

unread,
Jun 19, 2002, 2:51:17 AM6/19/02
to
> The __bases__ and __subclasses__ (and others) are listed in the 'type'
> object:
>
> >>> class Foo:
> ... pass
> ...
> >>> dir(type(type(Foo)))

Type is no longer a function, it is the type object itself.
You can just dir(type)

Oren


0 new messages