- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在通过属性来研究描述符协议(protocol),我正在这样写我自己的属性:
class my_property(object):
def __init__(self, fget=None, fset=None, fdel=None, doc=None):
self.fget = fget
self.fset = fset
self.fdel = fdel
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")
return self.fget(obj)
def setter(self, fset):
return type(self)(self.fget, fset, self.fdel)
class test_my_property(object):
def __init__(self, value):
self._val = value
@my_property
def val(self):
return self._val
@val.setter
def val(self, value):
self._val = value
def main():
c = test_my_property(5)
print c.val
c.val = 10
print c.val
print type(c).__dict__['val'].__set__
然后我得到:
5
10
AttributeError: 'my_property' object has no attribute '__set__'
我的问题是,既然“__set__”没有定义,那么“c.val = 10”如何工作?如果"__set__"是my_property从object继承的,那么,为什么会报AttributeError呢?
最佳答案
__set__
不是继承自对象。获取和设置 val
属性之所以有效,是因为在访问对象的属性时,首先会检查实例,然后检查类。由于您设置了实例属性 val
,它会使用它。如果您正在查看一个没有描述符的简单示例,我认为这一点尤其明显,
>>> class Foo(object):
... val = 5
...
>>> f = Foo()
>>> f.val # f doesn't have val so fallback on Foo
5
>>> f.val = 10
>>> f.val # f now has val so use it
10
>>> del f.val # oops what now
>>> f.val # class again
5
上面的例子和你的唯一区别是你的类 val
是(当你完成时)一个属性。
综上所述,您通常不希望将您的属性命名为与将保存其内容的实例属性相同的名称。通常的公式是这样的,
class Foo(object):
def __init__(self, value):
self._val = value
@property
def val(self):
return self._val
关于python - 为什么 instance.attribute = value 可以工作,而 __set__ 方法没有在属性中实现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30091273/
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 当我在服务器命令
我是一名优秀的程序员,十分优秀!