为什么财产只适用于物体? [英] Why property works only for objects?

查看:62
本文介绍了为什么财产只适用于物体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




以下代码显示,只有在课程中使用时,property()才有效。


---- --------------------------------------------

A级(对象):

通过


a = A()

ay = 7


def method_get(self):

返回self.y


ax = property(method_get)

print ax#=> <属性对象位于0xb7dc1c84>


A.x =属性(method_get)

打印a.x#=> 7

----------------------------------------- -------


是否有基于每个对象制作描述符的方法?我想

喜欢在A类的不同对象中自定义x访问权限。这可能是什么?b $ b可能以及如何?


mk < br $> b $ b -

。 o。 >> http://joker.linuxstuff.pl <<

。 。 o因为错误而得到宽恕更容易获得宽恕。

ooo而不是原谅正确。

解决方案

Michal Kwiatkowski写道:

下面的代码显示,只有在类中使用property()时,它才有效。


是的,描述符只适用于类级别(也就是说,只有

类对象调用__get__方法)。

是否有基于每个对象制作描述符的方法?




我仍​​然不相信你真的想要,但你可以写

你自己的描述符分派给实例对象(而不是

类型):

< blockquote class =post_quotes> class InstanceProperty(object):
.... def __init __(self,func_name):

.... self.func_name = func_name

.... def __get __(self,obj,type = None):

....如果obj是None:

....返回self

.... return getattr(obj,self.func_name)(obj)

.... class C(object):
... .x = InstanceProperty(''_ x'')

.... c = C()
cx
回溯(最近一次调用l ast):

文件"<互动输入>",第1行,在?

文件"< interactive input>",第7行,in __get__

AttributeError:''C''对象没有属性''_ x''def getx(self):
....返回42

。 ... c._x = getx
cx



42

STeVe

Steven Bethard napisa3(a):

是否有基于每个对象制作描述符的方法?



我仍然不相信你真的想要,但是你可以编写自己的描述符来调度实例对象(而不是
类型):




好​​的,这适用于我知道类定义名称的属性。什么

关于其他情况?我可能想要为类的实例设置不同的

属性集。还有可能吗?我不会像现在的解决方案那样
,因为它包装了一些东西,在这个过程中创建了另一个

属性(''_x'')。 Aren有没有更干净的解决方案?


问题是我有一个给定类的实例(比如BaseClass)而且我想要它实现
一些属性访问作为方法调用。我不是这个对象的
创建者,因此更改BaseClass的定义或

子类化它不是一个选项。下面的代码不起作用,但显示我的

意图:


#obj是BaseClass的实例

def get_x(自我):

#...

def set_x(自我,价值):

#...

obj.x = property(get_x,set_x)


更改对象的__setattr __ / __ getattr__也不适用于

同样的原因:字典查询仅在类属性中进行,

省略对象属性。这令人困惑,因为写obj.x和

obj .__ getattribute __(''x'')给出了不同的结果。似乎

也没有任何明确的方法来定义对象的方法。类似的东西:


class C(对象):

pass


def方法(个体经营):

返回self.x


c = cx

c.method =方法

c.method() #=> AttributeError


因此出现了另一个问题。是否可以使函数成为一个方法(因此它将接收调用对象作为第一个参数)?


mk

- -

。 o。 >> http://joker.linuxstuff.pl <<

。 。 o因为错误而得到宽恕更容易获得宽恕

ooo而不是原谅正确。


Michal Kwiatkowski< ru ** @ no .spam>写道:

...

问题是我有一个给定类的实例(比如BaseClass),我希望它实现一些属性访问方法调用。我不是这个对象的创建者,因此更改BaseClass的定义或继承它不是一个选项。


错了! _course_这是一个选项 - 为什么你认为这对你来说是否重要?

以下代码不是'工作,但显示我的意图:

#obj是BaseClass的实例
def get_x(个体经营):
#...
def set_x(self,value):
#...
obj.x = property(get_x,set_x)




def insert_property(obj, name,getter,setter):

class sub(obj .__ class__):pass

setattr(sub,name,property(getter,setter))

obj .__ class__ = sub


见?当然你可以继承 - 实际上并不难。

Alex


Hi,

Code below shows that property() works only if you use it within a class.

------------------------------------------------
class A(object):
pass

a = A()
a.y = 7

def method_get(self):
return self.y

a.x = property(method_get)
print a.x # => <property object at 0xb7dc1c84>

A.x = property(method_get)
print a.x # => 7
------------------------------------------------

Is there any method of making descriptors on per-object basis? I would
like to customize x access in different objects of class A. Is this
possible and how?

mk
--
. o . >> http://joker.linuxstuff.pl <<
. . o It''s easier to get forgiveness for being wrong
o o o than forgiveness for being right.

解决方案

Michal Kwiatkowski wrote:

Code below shows that property() works only if you use it within a class.
Yes, descriptors are only applied at the class level (that is, only
class objects call the __get__ methods).
Is there any method of making descriptors on per-object basis?



I''m still not convinced that you actually want to, but you can write
your own descriptor to dispatch to the instance object (instead of the
type):

class InstanceProperty(object): .... def __init__(self, func_name):
.... self.func_name = func_name
.... def __get__(self, obj, type=None):
.... if obj is None:
.... return self
.... return getattr(obj, self.func_name)(obj)
.... class C(object): .... x = InstanceProperty(''_x'')
.... c = C()
c.x Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "<interactive input>", line 7, in __get__
AttributeError: ''C'' object has no attribute ''_x'' def getx(self): .... return 42
.... c._x = getx
c.x


42
STeVe


Steven Bethard napisa3(a):

Is there any method of making descriptors on per-object basis?



I''m still not convinced that you actually want to, but you can write
your own descriptor to dispatch to the instance object (instead of the
type):



Ok, this works for attributes I know a name of at class definition. What
about other situations? I could possibly want to have different sets of
attributes for instances of a class. Is it still possible to do? I don''t
also like current solution, as it wraps things around, creating another
attribute (''_x'') in the process. Aren''t there any cleaner solutions?

The problem is I have an instance of a given class (say BaseClass) and I
want it to implement some attribute accesses as method calls. I''m not a
creator of this object, so changing definition of BaseClass or
subclassing it is not an option. Code below doesn''t work, but shows my
intention:

# obj is instance of BaseClass
def get_x(self):
# ...
def set_x(self, value):
# ...
obj.x = property(get_x, set_x)

Changing __setattr__/__getattr__ of an object also doesn''t work for the
same reason: dictionary lookup is made only in class attributes,
ommiting object attributes. It''s confusing, because writting obj.x and
obj.__getattribute__(''x'') gives a different results. There also seems
not to be any clear way to define methods for objects. Something like:

class C(object):
pass

def method(self):
return self.x

c = c.x
c.method = method
c.method() # => AttributeError

So another question arise. Is it possible to make function a method (so
it will receive calling object as first argument)?

mk
--
. o . >> http://joker.linuxstuff.pl <<
. . o It''s easier to get forgiveness for being wrong
o o o than forgiveness for being right.


Michal Kwiatkowski <ru**@no.spam> wrote:
...

The problem is I have an instance of a given class (say BaseClass) and I
want it to implement some attribute accesses as method calls. I''m not a
creator of this object, so changing definition of BaseClass or
subclassing it is not an option.
Wrong! Of _course_ it''s an option -- why do you think it matters at all
whether you''re the creator of this object?!
Code below doesn''t work, but shows my
intention:

# obj is instance of BaseClass
def get_x(self):
# ...
def set_x(self, value):
# ...
obj.x = property(get_x, set_x)



def insert_property(obj, name, getter, setter):
class sub(obj.__class__): pass
setattr(sub, name, property(getter, setter))
obj.__class__ = sub

See? Of COURSE you can subclass -- not hard at all, really.
Alex


这篇关于为什么财产只适用于物体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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