Skip to main content
 首页 » 编程设计

python之使用 Python @property 作为属性和方法

2025年02月15日7txw1958

我有一个用例,我希望 Python @property 在作为方法调用时(即末尾有括号)与在没有括号的情况下调用时表现不同.这样的事情有可能吗?

class Sequence: 
 
    @property 
    def first(self): 
        return self._first 
 
    @first.setter 
    def first(self, v): 
        self._first = v 
 
    # This won't work 
    @first.method 
    def first(self): 
        # Do something different than the setter and the getter since  
        # `first` is being called as a method. 
        return 4321 
 
 
seq = Sequence() 
seq.first = 1234 
 
# Setting and getting the first property works fine 
assert seq.first == 1234 
 
# Calling the first property as a function fails 
assert seq.first() == 4321 

请您参考如下方法:

好吧,您可以让 getter 返回一个 Proxy,它的行为类似于从 getter 返回的值,或者在您调用它时表现为可调用对象。如果您返回可从 getter 调用的内容,那将是模棱两可的,但在您的情况下,您确实返回了整数(不可调用)。它仍然不可取,但你可以让它工作(基于 the Python descriptor how-to 中提到的 property 模拟器):

def make_callable_proxy(val, call_func): 
    class CallableProxy(type(val)):  # subclass the class of value 
        __call__ = call_func 
 
    return CallableProxy(val) 
 
class CallableProperty(object): 
    def __init__(self, fget=None, fset=None, fdel=None, fcall=None, doc=None): 
        self.fget = fget 
        self.fset = fset 
        self.fdel = fdel 
        self.fcall = fcall 
        if doc is None and fget is not None: 
            doc = fget.__doc__ 
        self.__doc__ = doc 
 
    def __get__(self, obj, objtype=None): 
        if obj is None: 
            return self 
        if self.fget is None: 
            raise AttributeError("unreadable attribute") 
        # The following implements the "callable part". 
        if self.fcall is None: 
            return self.fget(obj) 
        value = make_callable_proxy(self.fget(obj), self.fcall) 
        return value 
 
    def __set__(self, obj, value): 
        if self.fset is None: 
            raise AttributeError("can't set attribute") 
        self.fset(obj, value) 
 
    def __delete__(self, obj): 
        if self.fdel is None: 
            raise AttributeError("can't delete attribute") 
        self.fdel(obj) 
 
    def getter(self, fget): 
        return type(self)(fget, self.fset, self.fdel, self.fcall, self.__doc__) 
 
    def setter(self, fset): 
        return type(self)(self.fget, fset, self.fdel, self.fcall, self.__doc__) 
 
    def deleter(self, fdel): 
        return type(self)(self.fget, self.fset, fdel, self.fcall, self.__doc__) 
 
    def method(self, fcall): 
        return type(self)(self.fget, self.fset, self.fdel, fcall, self.__doc__) 
 
class Sequence(object): 
 
    @CallableProperty 
    def first(self): 
        return self._first 
 
    @first.setter 
    def first(self, v): 
        self._first = v 
 
    # THIS WILL WORK NOW 
    @first.method 
    def first(self): 
        # Do something different than the setter and the getter since  
        # `first` is being called as a method. 
        return 4321 
 
 
seq = Sequence() 
seq.first = 1234 
 
# Setting and getting the first property works fine 
assert seq.first == 1234 
 
# Calling the first property as a function fails 
assert seq.first() == 4321 

这可以通过使用真正的代理类(如 wrapt.ObjectProxy )而不是 CallableProxy 类来进一步完善。但这取决于此类软件包的可用性。以防万一你有 wrapt 这就是它的样子:

from wrapt import ObjectProxy 
 
def make_callable_proxy(val, call_func): 
    class CallableProxy(ObjectProxy): 
        __call__ = call_func 
        __repr__ = val.__repr__  # just for a nicer representation 
 
    return CallableProxy(val)