accessor / mutator函数 [英] accessor/mutator functions

查看:198
本文介绍了accessor / mutator函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我看看如何在其他语言中设置类(例如C ++)时,我/ b $ b经常会观察到以下模式:

1)每个数据成员,该类将拥有一个访问者成员

函数(一个Get< what>函数)

2)对于每个数据成员,该类将具有一个mutator成员函数

(Set< whatver>函数)

3)数据成员永远不会被直接引用;它们始终是

引用了访问器和增变器功能


我的问题是:

a)以上三件事是否被认为是pythonic ?

b)使用getattr()和setattr()而不是

为每个数据成员创建访问器和增变器函数有什么权衡?

When I look at how classes are set up in other languages (e.g. C++), I
often observe the following patterns:
1) for each data member, the class will have an accessor member
function (a Get<whatever> function)
2) for each data member, the class will have a mutator member function
(a Set<whatver> function)
3) data members are never referenced directly; they are always
referenced with the accessor and mutator functions

My questions are:
a) Are the three things above considered pythonic?
b) What are the tradeoffs of using getattr() and setattr() rather than
creating accessor and mutator functions for each data member?

推荐答案

mi ***** *******@yahoo.com 写道:
当我看看如何用其他语言(例如C ++)设置类时,我经常观察以下模式:
1)对于每个数据成员,该类将具有一个访问者成员
函数(一个Get< what>函数)
2)对于每个数据成员,该类将具有mutator成员函数
(Set< whatver>函数)
3)数据成员永远不会直接引用;它们总是被访问器和增变器功能引用

我的问题是:
a)以上三件事是否被认为是pythonic?




b)使用getattr()和setattr()而不是为每个数据成员创建访问器和增变器函数的权衡是什么? ?
When I look at how classes are set up in other languages (e.g. C++), I
often observe the following patterns:
1) for each data member, the class will have an accessor member
function (a Get<whatever> function)
2) for each data member, the class will have a mutator member function
(a Set<whatver> function)
3) data members are never referenced directly; they are always
referenced with the accessor and mutator functions

My questions are:
a) Are the three things above considered pythonic?
No
b) What are the tradeoffs of using getattr() and setattr() rather than
creating accessor and mutator functions for each data member?




使用属性描述符代替:
http://www.python.org/2.2.1/descrintro.html#property
http://users.rcn.com/python/download...htm#properties


Michael



Use property descriptors instead:
http://www.python.org/2.2.1/descrintro.html#property
http://users.rcn.com/python/download...htm#properties

Michael


>我的问题是:
a)以上三件事是否被认为是pythonic?
a) Are the three things above considered pythonic?




Python使用一个名为''property的函数来实现同样的功能效果。

这个例子取自文档:


class C(object):

def __init __(self):

self .__ x = 0

def getx(个体经营):

返回自我.__ x

def setx( self,x):

如果x< 0:x = 0

self .__ x = x

x = property(getx,setx)

在上面的例子中,C( ).X"将调用并返回

..getx(访问者)和C()。x = 10的结果。会调用.setx(

mutator)。


它们被认为是Pythonic吗?好吧,这取决于它们的使用方式。

如果你使用属性进行某种类型的检查...

那么答案可能就是否定。 />

我发现它们对于实现惰性评估非常有用

属性,我认为这些属于Pythonic。


Sw。



Python uses a function called ''property to achieve the same effect.
This example is taken from the documentation:

class C(object):
def __init__(self):
self.__x = 0
def getx(self):
return self.__x
def setx(self, x):
if x < 0: x = 0
self.__x = x
x = property(getx, setx)
In the above example, "C().x" would invoke and return the result of
..getx (the accessor), and "C().x = 10" would invoke .setx (the
mutator).

Are they considered Pythonic? Well, It depends on how they are used.
If you are using properties to perform some kind of type checking...
then the answer is probably No.

I''ve found that they are quite useful for implementing lazy evaluation
of attributes, which I would consider quite Pythonic.

Sw.


如果类有两个属性--x和y - 代码看起来像

这样的东西喜欢这个:


C级(对象):

def __init __(个体经营):

self .__ x = 0

self .__ y = 0

def getx(self):

返回self .__ x

def setx(self,x) :

如果x< 0:x = 0

self .__ x = x

def gety(self):

返回自我.__ y

def sety(self,y):

如果y< 0:y = 0

self .__ y = y

x = property(getx,setx)

y = property(gety,sety)





因为如果是这样,延迟评估一词是指

而不是作者:


C()。getx()

C()。gety()

C()。setx( 10)

C()。sety(10)

可以替代:


C()。 x

C()。y

C()。x = 10

C()。y = 10




If the class had two attributes--x and y--would the code look like
something lik this:

class C(object):
def __init__(self):
self.__x = 0
self.__y = 0
def getx(self):
return self.__x
def setx(self, x):
if x < 0: x = 0
self.__x = x
def gety(self):
return self.__y
def sety(self, y):
if y < 0: y = 0
self.__y = y
x = property(getx, setx)
y = property(gety, sety)

?

Because if so, does the term ''lazy evaluation'' refer to the fact that
instead of:

C().getx()
C().gety()
C().setx(10)
C().sety(10)

one would substitute:

C().x
C().y
C().x = 10
C().y = 10

?


这篇关于accessor / mutator函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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