Py学习  »  Python

我可以在python 3中为描述符定义一个add方法吗?

Brad Fallon • 5 年前 • 488 次点击  

我在这里引用了python文档,以了解如何在控制getter和setter方法的python中创建类属性。 https://docs.python.org/3/howto/descriptor.html#id6

我用以下代码创建了一个描述符属性类:

class PropNumberString(object):
    def __init__(self, initval=None, name='var_int'):
        self.val = initval
        self.name = name

    def __get__(self, obj, objtype):
        print("in getter")
        return self.val

    def __set__(self, obj, val):
        print("in setter")
        self.val = int(val)

    def __add__(self, val):
        print("adding")
        print("val to add: ", val)
        self.val = int(self.val) + int(val)
        return self

    def __radd__(self, val):
        return self.__add__(val)


class ConnectionTemplate(object):
    tid = PropNumberString(0, "ID")

我也试图定义一个add方法,希望它总是添加一个整数,即使提供了一个字符串。这不起作用,似乎调用了“\uu get\uu”,然后调用返回整数的add方法,而不是我的add方法。

这里有一个测试来证明:

my_template = ConnectionTemplate()

print("Getting value")
print(my_template.tid)

print("Updating value")
my_template.tid = 100

print("Doing add")
my_template.tid = my_template.tid + "1"

print(my_template.tid)
print("type: ", type(my_template.tid))

输出:

Getting value
in getter
0
Updating value
in setter
Doing add
in getter
Traceback (most recent call last):
  File "connection_template.py", line 53, in <module>
    my_template.tid = my_template.tid + "1"
TypeError: unsupported operand type(s) for +: 'int' and 'str'

有好的解决办法吗?

编辑: 这就是我最后所做的,其中包含了亚历克斯·霍尔提供的答案

class StrInt(int):
    def __add__(self, val):
        return StrInt(super().__add__(int(val)))
    __radd__ = __add__

class PropNumberString(object):
    def __init__(self, val=None, name='var_name'):
        self.val = StrInt(val)
        self.name = name

    def __get__(self, obj, objtype):
        return self.val

    def __set__(self, obj, val):
        self.val = StrInt(val)

class ConnectionTemplate(object):
    tid_field = "ID"

    def __init__(self, tid=0):
        self.tid = PropNumberString(tid, self.ansa_tid_field)

    def __getattribute__(self, key):
        # see https://docs.python.org/3/howto/descriptor.html#id5
        "Emulate type_getattro() in Objects/typeobject.c"
        attrib = object.__getattribute__(self, key)
        if hasattr(attrib, '__get__'):
            return attrib.__get__(None, self)
        return attrib

    def __setattr__(self, key, val):
        try:
            attrib = object.__getattribute__(self, key)
        except AttributeError:
            self.__dict__[key] = val
            return
        if hasattr(attrib, '__set__'):
            attrib.__set__(None, val)
            return
        self.__dict__[key] = val

注: 我使用自定义描述符而不是属性装饰器的动机是希望我的属性能够包含自定义元数据,即json转储的键值。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/47413
 
488 次点击  
文章 [ 1 ]  |  最新文章 5 年前
Alex Hall
Reply   •   1 楼
Alex Hall    6 年前

不需要为此定义自定义描述符。你只需要 tid 返回一个添加了所需方式的类。最好是在设置时进行转换,而不是获取。下面是一个实现:

class MyInt(int):
    def __add__(self, val):
        return MyInt(super().__add__(int(val)))

    __radd__ = __add__


class ConnectionTemplate(object):
    def __init__(self):
        self._tid = 0

    @property
    def tid(self):
        return self._tid

    @tid.setter
    def tid(self, val):
        self._tid = MyInt(val)