Why are so many built-in types inheritable?

Georg Brandl g.brandl-nospam at gmx.net
Thu Mar 30 09:58:36 EST 2006


Antoon Pardon wrote:
> Op 2006-03-28, Georg Brandl schreef <g.brandl-nospam at gmx.net>:
>> Fabiano Sidler wrote:
>>> I really wanted to learn the reason for this, nothing else! ;)
>>
>> I suspect performance reasons. Can't give you details but function
>> is used so often that it deserves special treatment.
> 
> I would find this a bit odd. I think integers, tuples and lists
> are used just as often if not more and they aren't treated special.

Well, for integers, tuples and lists you can at least give useful
use cases for subclassing.

I now looked in the code, and the ability to subclass is given by a
flag in the type object. This is there in list or int, but not in function.

A little performance is saved by the fact that PyFunction_Check, which
verifies that an object is indeed a function, doesn't have to check for
subtypes.

So, if you want to make functions or slices subclassable, ask on python-dev!

Georg



More information about the Python-list mailing list