基于
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'
如何使用getattr
在一行中查找arms
对象的t
?谢谢
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
答案 0 :(得分:1)
x.obj.arms
等同于getattr(getattr(x, 'obj'), 'arms')
。
>>> x = Thing(Thing2())
>>> x.obj.arms
2
>>> getattr(getattr(x, 'obj'), 'arms')
2
或者,从Thing2
的实例开始,您只需编写
>>> t = Thing2()
>>> getattr(t, 'arms')
2
答案 1 :(得分:1)
你可以试试这个:
def set_session_attribute(obj, attribute):
if attribute.endswith('()'):
attribute = attribute.replace('()', '')
attrs = attribute.split('.')
for attr in attrs:
obj = getattr(obj, attr)
return obj
答案 2 :(得分:1)
试试这个:
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
def recursiveAttr(obj, attribute):
attributeHierarchy = attribute.split('.')
for attr in attributeHierarchy:
obj = getattr(obj, attr)
return obj
def get_session_attribute(obj, attribute):
if attribute.endswith('()'):
attribute = attribute.replace('()', '')
return recursiveAttr(obj, attribute)()
else:
return recursiveAttr(obj, attribute)
t = Thing2()
x = Thing(t)
print get_session_attribute(x, 'obj.arms')