status of foo.__get__ ?
Bengt Richter
bokr at oz.net
Mon May 26 14:25:07 EDT 2003
For python 2.2.2 at least, it appears that one can create a
sort of "bound function" analogous to bound methods from an ordinary function, e.g.,
>>> def foo(x,y):
... print 'foo called with x=%r, y=%r' % (x, y)
...
>>> foo2 = foo.__get__(2)
>>> foo2('xxx')
foo called with x=2, y='xxx'
>>> foo2
<bound method ?.foo of 2>
>>> foo2()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: foo() takes exactly 2 arguments (1 given)
Or should one use something like (untested beyond what you see here):
>>> def precurry(f, *args, **kw):
... def cufun(*cuargs, **cukw):
... kwuse = kw.copy()
... kwuse.update(cukw)
... return f(*(args+cuargs),**kwuse)
... return cufun
...
>>> hoo = precurry(foo, 'precurr')
>>> hoo('ha')
foo('precurr', 'ha')
and either implement that as a C extension or accept the overhead?
BTW, the overridable predefined keyword arg currying was just an extra
idea that I haven't at all thought through ;-)
>>> def foo(x,y,**kw): print 'foo(%r, %r, %r)' % (x,y,kw)
...
>>> hum = precurry(foo, 'precurr', hummer=123)
>>> hum('hum1')
foo('precurr', 'hum1', {'hummer': 123})
>>> hum('hum1', hummer='something else')
foo('precurr', 'hum1', {'hummer': 'something else'})
But the subject was the status of __get__ used with an ordinary function, and whether
that capability will continue or be prevented as abuse.
Regards,
Bengt Richter
More information about the Python-list
mailing list