property()用法 - 这是好的吗? [英] property() usage - is this as good as it gets?

查看:73
本文介绍了property()用法 - 这是好的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我想管理和控制对

a类中几个重要属性的访问,并覆盖其中某些属性的行为。 />
子类。


以下是我在当前的工作中实现这一点的剥离版本。


它运作良好,但我不能感觉有更清洁的更多

这样做的可读方式(减少重复等)。 />

这是不是很好,或者可以改进和改进

特别是如果我要添加一些相当多的属性

复杂的重载逻辑?


#!/ usr / bin / env python


A类(对象):

def __init __(个体经营):

self._x =无

self._y =无


def _set_x(self,value):

self._x = value


def _get_x(个体经营):

返回自我。 _x


x = property(_get_x,_set_x)


def _set_y(self,value):

self._y = value


def _get_y(self) :

返回self._y


y = property(_get_y,_set_y)


B类(A):

def __init __(自我):

self._z =无

super(B,self).__ init __()


def _set_x(self,x):

#一个示例子类''set''覆盖。

如果x == 10:

引发异常('%r无效!''%x)

self._x = x


x = property(A ._get_x,_set_x)


def _set_z(自我,价值):

self._z = value


def _get_z(个体经营):

返回self._z


z =物业(_get_z,_set_z)


del A._get_x,A._set_x,A._get_y,A._set_y

del B._set_x,B._get_z,B._set_z

Hi,

I want to manage and control access to several important attributes in
a class and override the behaviour of some of them in various
subclasses.

Below is a stripped version of how I''ve implemented this in my current
bit of work.

It works well enough, but I can''t help feeling there a cleaner more
readable way of doing this (with less duplication, etc).

Is this as good as it gets or can this be refined and improved
especially if I was to add in a couple more attributes some fairly
complex over-ride logic?

#!/usr/bin/env python

class A(object):
def __init__(self):
self._x = None
self._y = None

def _set_x(self, value):
self._x = value

def _get_x(self):
return self._x

x = property(_get_x, _set_x)

def _set_y(self, value):
self._y = value

def _get_y(self):
return self._y

y = property(_get_y, _set_y)

class B(A):
def __init__(self):
self._z = None
super(B, self).__init__()

def _set_x(self, x):
# An example subclass ''set'' override.
if x == 10:
raise Exception(''%r is invalid!'' % x)
self._x = x

x = property(A._get_x, _set_x)

def _set_z(self, value):
self._z = value

def _get_z(self):
return self._z

z = property(_get_z, _set_z)

del A._get_x, A._set_x, A._get_y, A._set_y
del B._set_x, B._get_z, B._set_z

推荐答案

8月22日,11:18 * am,David Moss< drk ... @ gmail.comwrote:
On Aug 22, 11:18*am, David Moss <drk...@gmail.comwrote:




我想管理和控制对

a类中几个重要属性的访问,并覆盖其中某些属性的行为

子类。


以下是我在当前

工作中实现这一点的剥离版本。


它运作良好,但我不能感觉有更清洁的更多

这样做的可读方式(减少重复等)。


这是不是很好,或者可以改进和改进

特别是如果我要添加一些相当多的属性

复杂的覆盖逻辑?


#!/ usr / bin / env python


A类(对象):

* * def __init __(个体经营):

* * * * self._x =无

* * * * self._y =无


* * def _set_x(自我,价值):

* * * * self._x =价值


* * def _get_x(self):

* * * * return self._x


* * x =属性(_get_x,_set_x)


* * def _set_y(self,value):

* * * * self._y = value


* * def _get_y(个体经营):

* * * *返回self._y


* * y = property(_get_y,_set_y)
Hi,

I want to manage and control access to several important attributes in
a class and override the behaviour of some of them in various
subclasses.

Below is a stripped version of how I''ve implemented this in my current
bit of work.

It works well enough, but I can''t help feeling there a cleaner more
readable way of doing this (with less duplication, etc).

Is this as good as it gets or can this be refined and improved
especially if I was to add in a couple more attributes some fairly
complex over-ride logic?

#!/usr/bin/env python

class A(object):
* * def __init__(self):
* * * * self._x = None
* * * * self._y = None

* * def _set_x(self, value):
* * * * self._x = value

* * def _get_x(self):
* * * * return self._x

* * x = property(_get_x, _set_x)

* * def _set_y(self, value):
* * * * self._y = value

* * def _get_y(self):
* * * * return self._y

* * y = property(_get_y, _set_y)



在Python的范围内,就是这样。


如果你没有在''get''或''set''上执行代码,那么

可能有一个默认的getter / setter,那只是设置变量名称的

值。这使得您的界面在继承和更改时保持相同。可以让它分配一个默认值




def _set_z(self,value):

self._z =价值


#def _get_z(个体经营):

#return self._z


z = property(True, _set_z)#still获取self._z


除非,您正在寻找定期或定期的内容。来自

我正在努力的事情:


def _getbalance(self):

返回self._tree.geti(

self.where + self.lookup [''balance''])


def _getparent(self):

返回自我._tree.getI(

self.where + self.lookup [''parent''])


其中''余额''和''父母' '可能都来自通用的东西:


balance = property(tree_lookup(''balance''))

parent = property(tree_lookup(''parent) ''))


他们必须小心谨慎地定义''self''和所有内容。

Within the bounds of Python, that''s it.

In the cases where you''re not executing code on ''get'' or ''set'', it
might be possible to have a default getter/setter, that just sets the
value of a variable name. This keeps your interface the same across
inheritance and changes. Possible to have it assign a default value
too.

def _set_z(self, value):
self._z = value

#def _get_z(self):
# return self._z

z = property( True, _set_z ) #still gets self._z

Unless, you are looking at something ''periodic'' or regular. From
something I''m working on:

def _getbalance( self ):
return self._tree.geti(
self.where+ self.lookup[ ''balance'' ] )

def _getparent( self ):
return self._tree.getI(
self.where+ self.lookup[ ''parent'' ] )

Where ''balance'' and ''parent'' could both come from something generic:

balance= property( tree_lookup( ''balance'' ) )
parent= property( tree_lookup( ''parent'' ) )

They would have to take care to define ''self'' properly and everything.


周五,2008年8月22日下午12:18,David Moss< dr **** @ gmail.comwrote:
On Fri, Aug 22, 2008 at 12:18 PM, David Moss <dr****@gmail.comwrote:




我想管理和控制对

a类中几个重要属性的访问,并覆盖其中一些属性的各种行为

su bclasses。


以下是我在目前的工作中实现这一点的剥离版本。


它运作良好,但我不能感觉有更清洁的更多

这样做的可读方式(减少重复等)。
Hi,

I want to manage and control access to several important attributes in
a class and override the behaviour of some of them in various
subclasses.

Below is a stripped version of how I''ve implemented this in my current
bit of work.

It works well enough, but I can''t help feeling there a cleaner more
readable way of doing this (with less duplication, etc).



当然可以更简洁,不改变含义:


###


类attrsetter(对象):

def __init __(self,attr):

self。 _attr = attr

def __call __(self,object,value):

setattr(object,self._attr,value)

def defaultproperty( attr):

返回属性(attrgetter(attr),attrsetter(attr))


A类(对象):

def __init __(self):

self._x =无

self._y =无


x = defaultproperty(''_ x '')

y = defaultproperty(''_ y'')


B级(A):

def __init __(自我):

super(B,self).__ init __()

self._z =无


def _set_x(self, x):

#一个示例子类''set''覆盖。

如果x == 10:

提高异常(''% r无效!''%x)

self._x = x


x = property(Axfget,_set_x)

z = defaultproperty(''_ z'')

#你真的不需要这样做,但你可以,如果你想要

del _set_x


###


但是,根据你给出的例子,你也可以这样写东西

(除了不再禁止删除属性,这真的是

不一定是必要的):


###


A级(对象):

def __init __(个体经营):

self.x =无

self.y =无


B级(A) :

def __init __(自我):

super(B,self).__ init __()

self._x = self.x

self.z =无


def _set_x(self,x):

#一个示例子类''set''覆盖。

如果x == 10:

加剧异常('%r无效!''%x)

self._x = x


x = property(attrgetter(''_ x''),_ se t_x)


###


如果没有
$,很难确切知道哪种方法最适合你b $ b更好地了解你的房产如何相互覆盖。


-Miles

It could certainly be made more concise, without changing the meaning:

###

from operator import attrgetter
class attrsetter(object):
def __init__(self, attr):
self._attr = attr
def __call__(self, object, value):
setattr(object, self._attr, value)
def defaultproperty(attr):
return property(attrgetter(attr), attrsetter(attr))

class A(object):
def __init__(self):
self._x = None
self._y = None

x = defaultproperty(''_x'')
y = defaultproperty(''_y'')

class B(A):
def __init__(self):
super(B, self).__init__()
self._z = None

def _set_x(self, x):
# An example subclass ''set'' override.
if x == 10:
raise Exception(''%r is invalid!'' % x)
self._x = x

x = property(A.x.fget, _set_x)
z = defaultproperty(''_z'')
# You really don''t need to do this, but you can if you want
del _set_x

###

But, given the example you gave, you could also write things this way
(aside from no longer forbidding deleting the properties, which really
shouldn''t be necessary):

###

class A(object):
def __init__(self):
self.x = None
self.y = None

class B(A):
def __init__(self):
super(B, self).__init__()
self._x = self.x
self.z = None

def _set_x(self, x):
# An example subclass ''set'' override.
if x == 10:
raise Exception(''%r is invalid!'' % x)
self._x = x

x = property(attrgetter(''_x''), _set_x)

###

It''s difficult to know exactly what would work best for you without a
better idea of how your properties override each other.

-Miles


8月22日,下午12:18 ,David Moss< drk ... @ gmail.comwrote:
On Aug 22, 12:18 pm, David Moss <drk...@gmail.comwrote:




我想管理和控制访问

a类中的几个重要属性,并在各种

子类中覆盖其中一些属性的行为。


下面是一个在我当前的

工作中我已经实现了这个版本的剥离版本。


它运作良好,但我感觉不舒服有一个更清洁的更多

这样做的可读方式(减少重复等)。


这是好还是可以改进改进了

特别是如果我要添加一些相当多的属性

复杂的 - 骑逻辑?
Hi,

I want to manage and control access to several important attributes in
a class and override the behaviour of some of them in various
subclasses.

Below is a stripped version of how I''ve implemented this in my current
bit of work.

It works well enough, but I can''t help feeling there a cleaner more
readable way of doing this (with less duplication, etc).

Is this as good as it gets or can this be refined and improved
especially if I was to add in a couple more attributes some fairly
complex over-ride logic?



就最重要而言,一个小小的改进就是OProperty的秘诀:

http://infinitesque.net/articles/2005/enhancing %20Python'的%20property.xhtml


乔治

A small improvement as far as overriding goes is the OProperty recipe:
http://infinitesque.net/articles/2005/enhancing%20Python''s%20property.xhtml

George


这篇关于property()用法 - 这是好的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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