A couple questions about classes and inheritance
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Sun May 16 19:55:33 EDT 2010
On Sun, 16 May 2010 18:58:45 -0400, Paul LaFollette wrote:
> First, I've looked a fair bit and can't find how one can find the base
> classes of a subclass?
subclass.__base__
subclass.__bases__
subclass.__mro__
(The third one stands for "Method Resolution Order".)
See also the inspect module.
> So, it appears that function is perhaps the equivalent of a Java final
> class? Or isn't really a class at all but something that "looks like" a
> class?
No, it really is a class, at least as far as Python is concerned:
>>> type(lambda: None)
<class 'function'>
>>> type(type(lambda: None))
<class 'type'>
but as you've discovered, it's also special. Unfortunately I can't find
any explanation of *why* (other than the obvious that subclassing doesn't
work).
> Anyway, again can you point me to somewhere that I can learn more? In
> particular, is there a list somewhere of the builtin types that are not
> subclassable?
I don't believe so. Apart from intellectual curiosity, is there any
reason you need to know?
This may be helpful:
http://www.python.org/dev/peps/pep-0253/
You can also google for the error message, although in my few minutes of
searching I've only found people demonstrating the failure and not
explaining it.
Here's a rather long discussion about subclassing function:
http://mail.python.org/pipermail/python-list/2008-March/531661.html
but again, no explanation of why type(function) isn't subclassable.
--
Steven
More information about the Python-list
mailing list