[Python-Dev] issue5578 - explanation
chris at simplistix.co.uk
Thu Apr 2 23:16:28 CEST 2009
R. David Murray wrote:
> On Wed, 1 Apr 2009 at 13:12, Chris Withers wrote:
>> Guido van Rossum wrote:
>>> Well hold on for a minute, I remember we used to have an exec
>>> statement in a class body in the standard library, to define some file
>>> methods in socket.py IIRC.
>> But why an exec?! Surely there must be some other way to do this than
>> an exec?
> Maybe, but this sure is gnarly code:
> _s = ("def %s(self, *args): return self._sock.%s(*args)\n\n"
> "%s.__doc__ = _realsocket.%s.__doc__\n")
> for _m in _socketmethods:
> exec _s % (_m, _m, _m, _m)
> del _m, _s
I played around with this and managed to rewrite it as:
from functools import partial
from new import instancemethod
for _m in _socketmethods:
p = partial(meth,_m)
p.__name__ = _m
p.__doc__ = getattr(_realsocket,_m).__doc__
m = instancemethod(p,None,_socketobject)
Have I missed something or is that a suitable replacement that gets rid
of the exec nastiness?
Simplistix - Content Management, Zope & Python Consulting
More information about the Python-Dev