M
Marc Aymerich
Hi,
I'm playing a bit with python dynamic methods and I came up with a
scenario that I don't understant. Considering the follow code:
# Declare a dummy class
class A(object):
pass
# generate a dynamic method and insert it to A class
for name in ['a', 'b', 'c']:
if name == 'b':
@property
def get_name(self):
return name
A.name = get_name
a_instance = A()
a_instance.name
# So far I exptect that a_instance.name returns 'b', since it has
been created when name == 'b', but this is what actually returns:
'c'
just the last 'name' value.
What can I do in order to generate a method like this but that returns
'b' ? What is wrong in my understanding of this pice of code?
Thanks !!!
marc
I'm playing a bit with python dynamic methods and I came up with a
scenario that I don't understant. Considering the follow code:
# Declare a dummy class
class A(object):
pass
# generate a dynamic method and insert it to A class
for name in ['a', 'b', 'c']:
if name == 'b':
@property
def get_name(self):
return name
A.name = get_name
a_instance = A()
a_instance.name
# So far I exptect that a_instance.name returns 'b', since it has
been created when name == 'b', but this is what actually returns:
'c'
just the last 'name' value.
What can I do in order to generate a method like this but that returns
'b' ? What is wrong in my understanding of this pice of code?
Thanks !!!
marc