Observer implementation question ('Patterns in Python')

Stargaming stargaming at gmail.com
Sat Oct 13 07:38:05 CEST 2007


On Fri, 12 Oct 2007 16:55:05 -0700, James Stroud wrote:

[snip]
> The use of __iadd__ & __isub__ as described in the article allows a neat
> shorthand, but does not function correctly in the context of new style
> classes.
> 
> James

But still, this call on a descriptor::

    obj.descr += val

is resolved into::

    descr.__get__(obj, obj.__class__).__iadd__(val)

under the condition that ``descr.__get__``'s return value supports 
`__iadd__`. After this is fully evaluated, `__set__` gets issued (as in 
``descr.__set__(obj, descr_get_iadd_stuff_here)``).

Besides, you could perfectly do this without any descriptors at all::

    class E(object):
        def __iadd__(self, val):
            print "__iadd__(%s, %s)" % (self, val)
    class C(object):
        e = E()
    c = C()
    c.e += 42
    # __iadd__(<__main__.E object at 0x...>, 42)

Adding `__call__` to this implementation should be easy.

HTH,
Stargaming



More information about the Python-list mailing list