Basé surne peut pas accéder à l'attribut w getattr d'un attribut?
In [65]: %paste
class Thing(object):
def __init__(self, obj):
self.legs = 4
self.obj = obj
def length(self):
return 6
class Thing2(object):
def __init__(self):
self.arms = 2
## -- End pasted text --
In [66]: t = Thing2()
In [67]: x = Thing(t)
In [68]: x.obj.arms
Out[68]: 2
In [69]: %paste
def set_session_attribute(obj, attribute):
if attribute.endswith('()'):
attribute = attribute.replace('()', '')
return getattr(obj, attribute)()
else:
return getattr(obj, attribute)
## -- End pasted text --
In [70]: x = set_session_attribute(x, 'obj.arms')
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-70-f8cbad3c39e5> in <module>()
----> 1 x = set_session_attribute(x, 'obj.arms')
<ipython-input-69-65b6efa81583> in set_session_attribute(obj, attribute)
4 return getattr(obj, attribute)()
5 else:
----> 6 return getattr(obj, attribute)
AttributeError: 'Thing' object has no attribute 'obj.arms'
Comment puis-je utiliser getattr
pour trouver arms
de l'objet t
en une ligne? Merci
class Thing(object):
def __init__(self, obj):
self.legs = 4
self.obj = obj
def length(self):
return 6
class Other(object):
def __init__(self):
self.arms = 2
def length(self):
return 5
def set_session_attribute(obj, attribute):
attrs = attribute.split('.')
for attribute in attrs:
if attribute.endswith('()'):
attribute = attribute.replace('()', '')
obj = getattr(obj, attribute)()
else:
obj = getattr(obj, attribute)
return obj
a = Other() # other has attr or function too
b = Thing(a) # thing has other obj, attr, and its own function
"""
In [80]: b.obj.length()
Out[80]: 5
In [81]: b.obj.arms
Out[81]: 2
In [83]: b.length()
Out[83]: 6
In [84]: b.legs
Out[84]: 4
"""
g = set_session_attribute(b, 'obj.length()')
h = set_session_attribute(b, 'obj.arms')
i = set_session_attribute(b, 'length()')
j = set_session_attribute(b, 'legs')
assert g == 5
assert h == 2
assert i == 6
assert j == 4
' L'objet Thing a un attribut nommé "bras", pas "obj.arms". – martineau
'obj.arms' n'est pas un attribut unique, c'est un attribut dans un attribut. – o11c
A quoi sert cette fonction 'set_session_attribute'? Quels formats doit-il gérer? Vous avez posé une question à propos de l'obtention de 'x.foo()' et d'une question sur l'obtention de la fonction 'x.foo.bar' maintenant; si elle doit gérer des choses comme 'x.foo [3] ('asdf'). bar' ou' x.foo + 3', nous pourrions aussi bien l'enlever maintenant. – user2357112