A couple questions about classes and inheritance

Paul LaFollette paul.lafollette at gmail.com
Sun May 16 18:58:45 EDT 2010


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:
>>>
>>> class MyFunction(function):
...       pass
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'function' is not defined
>>>

OK.. so second attempt:


>>>
>>> def a():
...       pass
...
>>> a
<function a at 0x00B65810>
>>> function
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'function' is not defined
>>> function=type(a)
>>> function
<class 'function'>
>>>
>>> class MyFunction(function):
...       pass
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: type 'function' is not an acceptable base type
>>>

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



More information about the Python-list mailing list