动态使用property()失败 [英] Dynamic use of property() fails

查看:61
本文介绍了动态使用property()失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




这是我第一次尝试新课程和动态python,所以我很可能会做一些非常愚蠢的事情... ...阅读后

描述符的方法在 http ://users.rcn.com/python/download/Descriptor.htm 我b $ b决定我会创建一个返回读取属性的对象,但是在

设置调用任意函数。


我的代码如下:

class ActiveDAO(object):

def __init __(self ):

self .__ values__ = {}

def add_field(self,name,value,on_change):

self .__ values __ [name] =值

def get(self):return self .__ values __ [name]

def set(self,new_value):self .__ values __ [name] =

on_change(new_value)

def delete(self):raise AttributeError

self .__ dict __ [name] = property(get,set,delete)


但是,当我尝试使用这个(在测试中)代码如下:

dao = ActiveDAO()

dao.add_field(" name"," value", lambda _:无)

assertEqual(dao.name," value")

我因为查找属性而返回

"<属性对象位于0x6b8910>"。


这是非常合理的,但我的表达方式是一些

魔术应该发生,如上面引用的文件中描述的




请有人解释为什么没有魔法? :o(


谢谢,

Andrew


PS我在Linux上使用Python 2.5

Hi,

This is my first attempt at new classes and dynamic python, so I am
probably doing something very stupid... After reading the how-to for
descriptors at http://users.rcn.com/python/download/Descriptor.htm I
decided I would make an object that returns attributes on read, but on
setting calls an arbitrary function.

My code looks like:
class ActiveDAO(object):
def __init__(self):
self.__values__ = {}
def add_field(self, name, value, on_change):
self.__values__[name] = value
def get(self): return self.__values__[name]
def set(self, new_value): self.__values__[name] =
on_change(new_value)
def delete(self): raise AttributeError
self.__dict__[name] = property(get, set, delete)

However, when I try to use this (in a test) with code like:
dao = ActiveDAO()
dao.add_field("name", "value", lambda _: None)
assertEqual(dao.name, "value")

I get a failure because lookup of the attribute is returning
"<property object at 0x6b8910>".

That is quite reasonable, but I was under the expression that some
magic was supposed to happen, as described in the document referenced
above!

Please can someone explain why there is no magic? :o(

Thanks,
Andrew

PS I am using Python 2.5 on Linux

推荐答案

En Mon,2008年4月14日22:43:39 -0300,andrew cooke< an **** @ acooke.org>
$ b $bescribió:
En Mon, 14 Apr 2008 22:43:39 -0300, andrew cooke <an****@acooke.org>
escribió:

这是我第一次尝试新的类和动态python,所以我可能会做一些非常愚蠢的事情。 ..在
描述符的操作方法之后_blank> http://users.rcn.com/python/download/Descriptor.htm
我确定我会创建一个在读取时返回属性的对象,但是在

设置调用任意函数。


我的代码如下:

class ActiveDAO(object):

def __init __(self):

self .__ values__ = {}

def add_field(self,name,value,on_change):

self。 __values_ _ [name] = value

def get(self):return self .__ values __ [name]

def set(self,new_value):self .__ values __ [name] =

on_change(new_value)

def delete(self):raise AttributeError

self .__ dict __ [name] = property(get,set,delete )


然而,当我尝试使用这个(在测试中)代码如下:

dao = ActiveDAO()

dao.add_field(" name"," value",lambda _:None)

assertEqual(dao.name," value")


我收到失败因为查找属性正在返回

"<属性对象位于0x6b8910>"。


这是非常合理的,但是我的表达方式是,应该发生一些

魔法,如上面引用的文件

所述!
This is my first attempt at new classes and dynamic python, so I am
probably doing something very stupid... After reading the how-to for
descriptors at http://users.rcn.com/python/download/Descriptor.htm I
decided I would make an object that returns attributes on read, but on
setting calls an arbitrary function.

My code looks like:
class ActiveDAO(object):
def __init__(self):
self.__values__ = {}
def add_field(self, name, value, on_change):
self.__values__[name] = value
def get(self): return self.__values__[name]
def set(self, new_value): self.__values__[name] =
on_change(new_value)
def delete(self): raise AttributeError
self.__dict__[name] = property(get, set, delete)

However, when I try to use this (in a test) with code like:
dao = ActiveDAO()
dao.add_field("name", "value", lambda _: None)
assertEqual(dao.name, "value")

I get a failure because lookup of the attribute is returning
"<property object at 0x6b8910>".

That is quite reasonable, but I was under the expression that some
magic was supposed to happen, as described in the document referenced
above!



魔法在* class *中找到描述符时发生,而不是在实例中的
中找到。我认为它在Hettinger的文档中有详细说明。

你真的想要每个实例吗?定义的属性?

__special__名称保留给Python内部使用;不要使用它们。

仅实现属性(私有属性)拼写为单个

下划线。

- -

Gabriel Genellina

The "magic" happens when the descriptor is found in the *class*, not in
the instance. I think it''s detailed in Hettinger''s document.
Do you actually want "per-instance" defined properties?

__special__ names are reserved for Python internal usage; don''t use them.
Implementation-only attributes ("private" ones) are spelled with a single
underscore.
--
Gabriel Genellina


[Gabriel:]
[Gabriel:]

魔术在* class *中找到描述符时发生,而不是在实例中的
中找到。我认为它在Hettinger的文档中有详细说明。

你真的想要每个实例吗?定义属性?
The "magic" happens when the descriptor is found in the *class*, not in
the instance. I think it''s detailed in Hettinger''s document.
Do you actually want "per-instance" defined properties?



啊!好。是的,你是对的:我想要实例值,但是类

属性,所以我会重新考虑事情。非常感谢!

ah! ok. yes, you''re right: i want instance values but class
properties, so i''ll rethink things. thanks very much!


__special__名称保留给Python内部使用;不要使用它们。

仅实现属性(私有属性)拼写为单个

下划线。
__special__ names are reserved for Python internal usage; don''t use them.
Implementation-only attributes ("private" ones) are spelled with a single
underscore.



啊,是的,对不起。


再次感谢您的快速回复,

andrew

ah, yes, sorry about that.

thanks again for the quick reply,
andrew


" Gabriel Genellina" < ga ******* @ yahoo.com.arwrites:
"Gabriel Genellina" <ga*******@yahoo.com.arwrites:

魔法当在* class *中找到描述符时发生,而不是在实例中找到
。我认为这在Hettinger的文件中有详细说明。
The "magic" happens when the descriptor is found in the *class*, not
in the instance. I think it''s detailed in Hettinger''s document.



这里的文件不对:


或者,更常见的是调用描述符
属性访问时自动
。例如,obj.d在obj字典中查找d

。如果d定义方法__get__,则

d .__ get __(obj)根据列出的优先规则调用

如下。


这听起来似乎有道理,可能导致Andrew相信他的

代码应该可行。它应该代之以在obj'字典中的'/ b $ b类型'或者那种效果。我问雷蒙德有一段时间

之前,他同意这是一个错误,但他显然没有得到

来修复它。 br />

文档中的实际代码示例当然是正确的。

The document is wrong here:

Alternatively, it is more common for a descriptor to be invoked
automatically upon attribute access. For example, obj.d looks up d
in the dictionary of obj. If d defines the method __get__, then
d.__get__(obj) is invoked according to the precedence rules listed
below.

This sounds plausible and might have led Andrew to believe that his
code should work. It should instead say "in the dictionary of obj''s
type" or something to that effect. I asked Raymond about it some time
ago, and he agreed that it''s an error, but he apparently didn''t get
around to fixing it.

The actual code examples in the document are, of course, correct.


这篇关于动态使用property()失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆