[Python-Dev] Re: accumulator display syntax

Alex Martelli aleaxit at yahoo.com
Fri Oct 17 17:08:04 EDT 2003


On Friday 17 October 2003 10:52 pm, Paul Moore wrote:
   ...
> >>     selector = Top.__get__(10)
   ...
> Urk. I just checked, and this works. But I haven't the foggiest idea
> why! Could someone please explain? If you do, I promise never to
> reveal who told me :-)

Functions are descriptors, and func.__get__(obj) returns a bound
method with im_self set to obj -- that's how functions become bound
methods, in today's Python, when accessed with attribute syntax
obj.func on an instance obj of a class which has func in its dict.
But the mechanism is NOT meant for general currying... you could
say the latter just works as a weird-ish side effect, and not in too
general a way: consider for example:

>>> def p(s): print s
...
>>> p.__get__('one case').__get__('another')()
another
>>>

the second __get__ "replaces" the im_self [[it works on _p_ again,
the im_func of the bound method given by the first, NOT on "the
bound method itself", as that isn't a descriptor]]... now if we had a
marketing dept it could sell this as a feature, "rebindable curried
functions", perhaps, but in fact it's an "accidental side effect"...;-)


Alex






More information about the Python-Dev mailing list