Context manager with class methods
greg.ewing at canterbury.ac.nz
Sat Sep 24 02:07:54 CEST 2011
Terry Reedy wrote:
> it is normal to look for special methods on the class (and superclasses)
> of an object rather than starting with the object itself.
> I suspect there was a deliberate change to correct an anomaly, though
> this might have been done as part of some other change.
It's a necessary consequence of the fact that new-style classes
are also instances. Without it, there would be an ambiguity as
to whether a special method defined the behaviour of instances
of a class or of the class object itself.
It also increases efficiency, because for those special methods
that correspond to C-level type slots, you only have to look
in the type slot to find an implementation of the method,
rather than having to look in the instance dict first.
More information about the Python-list