[Cython] PEP 3135 -- New Super

Stefan Behnel stefan_ml at behnel.de
Tue Jul 5 10:49:42 CEST 2011

Vitja Makarov, 05.07.2011 10:37:
> 2011/7/5 Stefan Behnel:
>> IMO, the main reason for the heuristic is to prevent class objects from
>> being kept alive by their methods, except for the single case where super()
>> is used. Keeping a class alive just because one of its methods is still used
>> somewhere can be very costly, depending on the content of the class dict. It
>> also creates a reference cycle, which is another costly thing in CPython as
>> it requires a GC run over the whole class dict to get cleaned up.
>> The situation for modules is at least slightly different, as modules do not
>> tend to get unloaded, so there's always a reference to them from
>> sys.modules. If that ever gets removed, it's really ok if it takes time to
>> clean things up.
> Yes, but I hope that classes are rarely deleted.
> And I'm afraid that we can't avoid cycles when implementing __class__
> for pure-python classes.

As I said, it's fine if super() is used, but not otherwise. Basically, 
no-args super() is just a shortcut for "super(TheClass, self)", with the 
difference that the long form looks up the name at runtime, whereas the 
short form keeps a reference to the type at module setup time. So, both 
need the class at runtime, but have different characteristics otherwise.

(but if the code is not using super(), there is no need to have the methods 
own a reference to their class)


More information about the cython-devel mailing list