使用一个函数来获取函数或常规属性的getattr

cod*_*321 2 python getattr python-3.x getattribute

我有以下代码:

In [38]: %paste
def set_session_attribute(obj, attribute):
    if attribute.endswith('()'):
        attribute = attribute.replace('()', '')
        return getattr(obj, attribute)()
    else:
        return getattr(obj, attribute)


class Thing(object):
    def __init__(self):
        self.legs = 4
    def length(self):
        return 6

## -- End pasted text --

In [39]: x = Thing()

In [40]: y = set_session_attribute(x, 'legs')

In [41]: y
Out[41]: 4

In [42]: z = set_session_attribute(x, 'length()')

In [43]: z
Out[43]: 6
Run Code Online (Sandbox Code Playgroud)

这是因为调用"length()"不起作用(AttributeError, no attribute length())

是否有一种更短,更易于维护的方式来制作这样的功能?谢谢.

Mik*_*ler 5

解决方案1

你可以做length一个property:

class Thing(object):
    def __init__(self):
        self.legs = 4
    @property
    def length(self):
        return 6

>>> thing = Thing()
>>> thing.legs
4
>>> thing.length
6
Run Code Online (Sandbox Code Playgroud)

如果你真的想使用你的功能:

def set_session_attribute(obj, attribute):
    return getattr(obj, attribute)

>>> set_session_attribute(thing, 'legs')
4
>>> set_session_attribute(thing, 'length')
6
Run Code Online (Sandbox Code Playgroud)

如果无法thing直接更改源,可以在导入类后执行:

class Thing(object):
    def __init__(self):
        self.legs = 4
    def length(self):
        return 6
Run Code Online (Sandbox Code Playgroud)

这里:

Thing.length2 = property(Thing.length)

>>> thing = Thing()
>>> thing.length2
6
Run Code Online (Sandbox Code Playgroud)

解决方案2

或者,您可以检查属性是否可调用:

class Thing(object):
    def __init__(self):
        self.legs = 4
    def length(self):
        return 6

def set_session_attribute(obj, attribute):
    attr = getattr(obj, attribute)
    if hasattr(attr, '__call__'):  # may use `callable()`
        return attr()
    return attr

>> thing = Thing()
>>> set_session_attribute(thing, 'legs')
4
>>> set_session_attribute(thing, 'length')
6
Run Code Online (Sandbox Code Playgroud)