A couple questions about classes and inheritance

P

Paul LaFollette

Kind people,
Using Python 3.1

I have been poking around trying to get more insight into Python's
innards, and I have a couple of (marginally) related questions.

First, I've looked a fair bit and can't find how one can find the base
classes of a subclass? isinstance and issubclass sort of do the
opposite of what I want. Surely somewhere there is something like

MyThingee.something.orOther.baseClasses()

that returns a tuple of the parents? If there is, can you point me to
the appropriate documentation?

Second,
I can subclass most builtin things like int and float and list and so
on, and then override operators to play Twilight Zone if I am so
inclined.

But what happens if I try to subclass function?

First attempt:.... pass
....
Traceback (most recent call last):

OK.. so second attempt:

.... pass
....Traceback (most recent call last):
.... pass
....
Traceback (most recent call last):

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?

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'm willing to root through the source code if that is what I need to
do, but even there (especially there) a hint as to where to start
looking would be helpful.

Thank you in advance,
Paul
 
S

Steven D'Aprano

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:
<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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top