- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我只是想使用描述符模式,但似乎效果不佳。这是一个简短的例子(没有任何实际用途,只是为了展示):
class Num(object):
def__init__(self, val=0):
self.val = val
def __get__(self, instance, owner):
return self.val
def __set__(self, instance, val):
self.val = val
def __str__(self):
return "Num(%s)" % self.val
def __repr__(self):
return self.__str__()
class Test(object):
def __init__(self, num=Num()):
self.num = num
和测试:
>>>t = Test()
>>>t.num # OK
Num(0)
>>>t.num + 3 #OK i know how to fix that, but I thought __get__.(t.num, t, Test) will be called
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'Num' and 'int'
>>> t.num = 4 # why isn't __set__(t.num, t, 4) called here?
>>> t.num
4
我的误解是什么?
最佳答案
描述符只有在它们是类的属性时才起作用,而不是实例。如果您将类(class)更改为:
class Test(object):
num = Num()
. . .然后描述符将起作用。
但是,因为必须在类上设置描述符,这意味着描述符只有一个实例,所以描述符将其值存储在 self
上可能不是一个好主意.这些值将在该类的所有实例之间共享。相反,在 instance
上设置值。
另外,请注意您的 __str__
和 __repr__
可能不会按照您的预期进行。调用 t.num
将激活描述符并返回其 val
,因此 t.num
的结果将是普通数字 0,而不是 a Num
个实例。描述符的全部要点是透明地返回 __get__
的结果,而不使描述符对象本身可见。
这里有一些说明性的例子:
>>> t1 = Test()
>>> t2 = Test()
>>> t1.num
0
>>> Test.num
0
# Accessing the descriptor object itself
>>> Test.__dict__['num']
Num(0)
>>> t1.num = 10
>>> t1.num
10
# setting the value changed it everywhere
>>> t2.num
10
>>> Test.num
10
使用描述符的替代版本:
class Num(object):
def __init__(self, val=0):
self.val = val
def __get__(self, instance, owner):
try:
return instance._hidden_val
except AttributeError:
# use self.val as default
return self.val
def __set__(self, instance, val):
instance._hidden_val = val
class Test(object):
num = Num()
>>> t1 = Test()
>>> t2 = Test()
>>> t1.num
0
>>> t1.num = 10
>>> t1.num
10
# Now there is a separate value per instance
>>> t2.num
0
关于python - 让 python __set__ 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27029441/
class A(object): def __get__(self, instance, owner):#why i can't find argument 'key',where is 'k
我遵循了“python cookbook”关于“实现数据模型或类型系统”主题的建议,代码如下: class Descriptor(object): def __init__(self, nam
我的数据描述符适用于同时具有 __set__ 和 __get__ 的对象。 但是,类描述符似乎不支持 __set__。这样做会用分配的值替换描述符对象本身。 下面的代码演示了这一点 from __fu
我只是想使用描述符模式,但似乎效果不佳。这是一个简短的例子(没有任何实际用途,只是为了展示): class Num(object): def__init__(self, val=0): s
我正在创建一个自定义数据迁移,以根据两个不同模型中的现有条目自动在数据库中创建 GenericRelation 条目。 示例 models.py: ... class Place content
我确定这将被标记为重复,但我真的不明白我在看什么。我已经查看了有关描述符的 Python 文档,但我已经用 Python 进行了两周的“编程”,但我真的不知道我在寻找什么! 这是我得到的: >>> c
我在解决我正在查看的问题时遇到了一些问题。我有一组专门的函数将在程序中使用,它们基本上是可以替换函数和方法的动态可调用函数。由于需要让它们正常工作以模拟方法的功能,这些函数会覆盖 __get__提供一
我有一个类的描述符,但它的 __set__ 方法没有被调用。几个小时以来,我一直在努力寻找这个问题,但对此没有任何答案。但我在下面注意到的是,当我将 12 分配给 MyTest.X 时,它会删除 X
来自 https://docs.python.org/2/howto/descriptor.html : descr.__get__(self, obj, type=None) --> value d
我在某处读到过这样一个事实,你可以使用 __set__ 而没有 __get__ 的描述符。 它是如何工作的? 它算作数据描述符吗?是非数据描述符吗? 这是一个代码示例: class Desc:
代码: class Pizza: def __init__(self, toppings): self.toppings = toppings self._pineapple_al
这个问题已经有答案了: Understanding __get__ and __set__ and Python descriptors (8 个回答) 已关闭 5 年前。 假设我们定义以下Pytho
来自 Python data model documentation : object.__get__(self, instance, owner=None) Called to get the at
问:为什么 hh.a = 2 不是我期望的,但 b.a = 2 可以? 代码如下: class D: def __init__(self): print('this is in
假设我有这个 Python descriptor : class MyDesc(object): def __get__(self, instance, instance_type=None)
正如标题所说。 来自 Java 我曾经: private int A; public void setA(int A) { this.A = A; } public int getA() {
我正在尝试了解 Python 的描述符是什么以及它们有什么用处。我了解它们的工作原理,但这是我的疑问。考虑以下代码: class Celsius(object): def __init__(s
我正在通过属性来研究描述符协议(protocol),我正在这样写我自己的属性: class my_property(object): def __init__(self, fget=None,
我在以下远程服务器设置上使用 Python 3.6.5: Server: Windows 10 Python: 3.6.5 Requests: 2.18.4 Pentaho: 8.0 当我在服务器命令
我是一名优秀的程序员,十分优秀!