[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

adding class functionality, nested scoping

jholg

1/4/2008 10:44:00 AM

Hi,

regarding automatically adding functionality to a class (basically taken
from the cookbook recipee) and Python's lexical nested scoping I have a question wrt this code:

#-----------------
import types

# minor variation on cookbook recipee
def enhance_method(cls, methodname, replacement):
'replace a method with an enhancement'
method = getattr(cls, methodname)
def _f(*args, **kwargs):
return replacement(method, *args, **kwargs)
_f.__name__ = methodname
setattr(cls, methodname, types.MethodType(_f, None, cls))

# loop over class dict and call enhance_method() function
# for all methods to modify
def enhance_all_methods(cls, replacement):
for methodname in cls.__dict__:
if not methodname.startswith("__"):
method = getattr(cls, methodname)
def _f(*args, **kwargs):
return replacement(method, *args, **kwargs)
_f.__name__ = methodname
enhance_method(cls, methodname, replacement)


# Does not work: all enhanced methods only call the last wrapped originial
# method. It seems the name 'method' in the surrounding scope of the
# def _(...) function definition only refers to the last loop value(?)
def ERRONEOUS_enhance_all_methods(cls, replacement):
for methodname in cls.__dict__:
if not methodname.startswith("__"):
method = getattr(cls, methodname)
def _f(*args, **kwargs):
return replacement(method, *args, **kwargs)
_f.__name__ = methodname
setattr(cls, methodname, types.MethodType(_f, None, cls))


class Foo(object):
def foo(self, x):
print "foo", x

def bar(self, x):
print "bar", x


def logme(method, *args, **kwargs):
print "-->", method.__name__, args, kwargs
try:
return method(*args, **kwargs)
finally:
print "<--"


#enhance_all_methods(Foo, logme)
ERRONEOUS_enhance_all_methods(Foo, logme)

foo = Foo()
foo.foo(2)
foo.bar(2)
#-----------------

....give this output:
>>> foo = Foo()
>>> foo.foo(2)
--> foo (<__main__.Foo object at 0x1b08f0>, 2) {}
foo 2
<--
>>> foo.bar(2)
--> foo (<__main__.Foo object at 0x1b08f0>, 2) {}
foo 2
<--
>>>

So, while using enhance_all_methods() to add functionality does work, ERRONEOUS_enhance_all_methods() does not. Why is this? Is the explanation I tried to give in the code comment on the right track:

# Does not work: all enhanced methods only call the last wrapped originial
# method. It seems the name 'method' in the surrounding scope of the
# def _(...) function definition only refers to the last loop value(?)

Thanks for any hint,
Holger
--
Psssst! Schon vom neuen GMX MultiMessenger gehört?
Der kann`s mit allen: http://www.gmx.net/de/go/multimessen...
1 Answer

Arnaud Delobelle

1/4/2008 11:20:00 AM

0

On Jan 4, 10:43 am, jh...@gmx.de wrote:
> Hi,

Hi

[...]
> # Does not work: all enhanced methods only call the last wrapped originial
> # method. It seems the name 'method' in the surrounding scope of the
> # def _(...) function definition only refers to the last loop value(?)
> def ERRONEOUS_enhance_all_methods(cls, replacement):
>     for methodname in cls.__dict__:
>         if not methodname.startswith("__"):
>             method = getattr(cls, methodname)
>             def _f(*args, **kwargs):
>                 return replacement(method, *args, **kwargs)
>             _f.__name__ = methodname
>             setattr(cls, methodname, types.MethodType(_f, None, cls))
>

This is normal: After ERRONEOUS_enhance_all_methods is called, the
value method is the one from the last iteration of the loop. All
subsequent references to 'method' (in function _f) will return that
last value. To solve this problem you need to fix the object 'method'
is bound to in function _f:

def enhance_all_methods(cls, replacement):
# The following binds 'method' to its current value in _f
def replace(method):
def _f(*args, **kwargs):
return replacement(method, *args, **kwargs)
return _f
for methodname in cls.__dict__:
if not methodname.startswith("__"):
_f = replace(getattr(cls, methodname))
_f.__name__ = methodname
setattr(cls, methodname, types.MethodType(_f, None, cls))

Of course this looks more like your first version, which trims down to
the following and is probably a better option:

def enhance_all_methods(cls, replacement):
for methodname in cls.__dict__:
if not methodname.startswith("__"):
enhance_method(cls, methodname, replacement)

HTH

--
Arnaud