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?
> 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
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
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
Type is no longer a function, it is the type object itself.
You can just dir(type)
Oren