Magic methods in extension types

Jacek Generowicz jacek.generowicz at cern.ch
Mon Apr 26 08:07:07 EDT 2004


Michael Hudson <mwh at python.net> writes:

> Not finding the tp_as_number->nb_inplace_add field?

( ... or tp_as_sequence ... )

I was afraid you (someone) was going to say that.

> I think *all* magic methods correspond to slots in (or near) the type
> object -- it's practically the definition of "magic method"!

Hmmm

    >>> class foo:
    ...     def __iadd__(self,other):
    ...         print "__iadd__ running"
    ...         return self
    ... 
    >>> f = foo()
    >>> f += 2
    __iadd__ running

I'd be surprised if I've added __iadd__ to a type object here, yet it
seems to work.

Python manages to map "+=" to the method called "__iadd__" in
user-defined classes, but not for extension types. What is the
essential difference that makes that mapping work in one case but not
in the other?




More information about the Python-list mailing list