关于** args的使用 [英] About the use of **args

查看:87
本文介绍了关于** args的使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述






我正在开始一个新的项目,我对不同的接口有疑问

为了我的clases。我的clases将有很多属性,我想要知道什么样的aproach可能是最好的


1)定义一个SetAttribute / GetAttribute对每个

属性的方法。

2)定义一个SetAttribute / GetAttribute,其中一个参数是一个键=值

格式。


任何优惠?


提前致谢


Zunbeltz Izaola


-

从电子邮件中删除XXX: zu ****** @ wm .lc.ehu.esXX X


Hi

I''m starting a new proyect and i''m in doubt about diferent interfaces
for my clases. My clases will have a lot of attributes and i''m want to
know what aproach could be the best

1) Define one SetAttribute/GetAttribute pair of method for each
attribute.
2) Define one SetAttribute/GetAttribute which argument is a key=value
format.

Any advaice?

Thanks in advance

Zunbeltz Izaola

--
Remove XXX from email: zu******@wm.lc.ehu.esXXX

推荐答案

2003年12月10日星期三09:38:55 +0100,Zunbeltz Izaola写道:
On Wed, 10 Dec 2003 09:38:55 +0100, Zunbeltz Izaola wrote:
我正在开始一个新的项目,我对我的clases的不同界面有疑问。我的clases将有很多属性,我想知道什么样的aproach可能是最好的

1)为每个定义一个SetAttribute / GetAttribute方法
属性。
2)定义一个SetAttribute / GetAttribute,哪个参数是一个key = value
格式。
I''m starting a new proyect and i''m in doubt about diferent interfaces
for my clases. My clases will have a lot of attributes and i''m want to
know what aproach could be the best

1) Define one SetAttribute/GetAttribute pair of method for each
attribute.
2) Define one SetAttribute/GetAttribute which argument is a key=value
format.




我自己问了一个类似的问题从Python开始,我得到了一些非常好的答案,这些答案很适合我。

http://groups.google.com/groups?hl=e...net%26rnum%3D2


我的首选技术是直接在对象上设置属性,

如下:


shoe =鞋子()

shoe.laced_up =真

shoe.is_on_foot =真

shoe.resoled = False


您可能认为它有点讨厌,因为它破坏了封装等等,但是
但是Python旨在让您快速,干净地完成工作。你是
并不总是在你需要强制访问的情况下,所以

严格。直接使用属性更像是一个Python习惯用法(请参阅我之前发布的帖子中的最后两篇文章的

)。


因为早期的线程Python已经被给出了一个很好的方式

自定义属性访问;属性。基本上,他们让你

定义当你访问/分配

属性时运行的get / set方法。你可以通过为你的班级获得一个更清晰的界面而受益,并且你需要在实际需要时定义get / set方法(例如,当他们有副作用时,他们需要
)比获取/设置属性更好。


例如:


class鞋子:


def __init __(self):

self._laced_up = False

self.is_on_foot = False

self.resoled = False


def _set_laced_up(self,boolean):

self._laced_up = boolean

如果布尔值:

self.is_on_foot =真


def _get_laced_up(个体经营):

返回self._laced_up


laced_up = property(_get_laced_up, _set_laced_up)


我没有运行它,因此它可能有语法错误。它应该说明

原则;您可以直接使用属性,直到您想要设置它们时采取

操作。然后,您可以将属性设为私有并且

将其替换为访问真实属性的属性,并且当访问它时,无论你想要做什么,都可以使用它。

希望有意义。


- 格雷厄姆



I asked a similar question myself when I started out with Python, and I
got some very good answers that served me well.

http://groups.google.com/groups?hl=e...net%26rnum%3D2

My preferred technique is to just set attributes directly on the object,
like this:

shoe = Shoe()
shoe.laced_up = True
shoe.is_on_foot = True
shoe.resoled = False

You may think that it''s a bit nasty because it breaks encapsulation, etc.,
but Python is designed to let you do stuff quickly and cleanly. You''re
not always in a situation where you need to enforce access so
strictly. Using attributes directly is more of a Python idiom (see the
last two posts in the thread I posted earlier).

Since that earlier thread Python has been given a nice way of
customising attribute access; properties. Basically, they let you
define get/set methods that get run when you access/assign to an
attribute. You benefit by getting a cleaner interface to your class, and
you only have to define get/set methods when you actually need them (i.e.
when they have side effects other than getting/setting the attribute).

For example:

class Shoe:

def __init__(self):
self._laced_up = False
self.is_on_foot = False
self.resoled = False

def _set_laced_up(self, boolean):
self._laced_up = boolean
if boolean:
self.is_on_foot = True

def _get_laced_up(self):
return self._laced_up

laced_up = property(_get_laced_up, _set_laced_up)

I''ve not run that so it may have syntax errors. It should illustrate the
principle though; you can use attributes directly until you want to take
actions when you set them. Then you can make the attribute "private" and
replace it with a property that access the real attribute, and does
whatever else you want to do when it''s accessed.

Hope that makes sense.

-- Graham




" Zunbeltz Izaola <つ****** @ wm.lc.ehu.es.XXX>在消息中写道

news:m1 ************ @ lcpxdf.wm.lc.ehu.es ...

"Zunbeltz Izaola" <zu******@wm.lc.ehu.es.XXX> wrote in message
news:m1************@lcpxdf.wm.lc.ehu.es...



我正在开始一个新的项目,我对我的clases的不同界面有疑问。我的clases将有很多属性,我想知道什么样的aproach可能是最好的

1)为每个定义一个SetAttribute / GetAttribute方法
属性。
2)定义一个SetAttribute / GetAttribute,哪个参数是一个key = value
格式。

任何优点?

提前致谢

Zunbeltz Izaola


我认为你在谈论构建对象,

不是关于状态变化之后的状态构建并且

释放到野外?


在这种情况下,最佳是模式是永远不会让对象

出现一半构造。有很多种方法可以实现这一点,从将所有需要的参数传递到

的构造函数,到工厂的众多变化

模式。并且我不会对另一个响应者'

简单地将值插入到实例中的做法不利,而

它仍然在工厂中:大多数真实世界的物体不包含

它们构造的逻辑,那么我们为什么要编程

对象?


John Roth

Hi

I''m starting a new proyect and i''m in doubt about diferent interfaces
for my clases. My clases will have a lot of attributes and i''m want to
know what aproach could be the best

1) Define one SetAttribute/GetAttribute pair of method for each
attribute.
2) Define one SetAttribute/GetAttribute which argument is a key=value
format.

Any advaice?

Thanks in advance

Zunbeltz Izaola
I presume you''re talking about constructing the object,
not about state changes after it''s been constructed and
released into the wild?

In that case, the "best" pattern is to never let the object
appear half constructed. There are a wide variety of ways
of doing this, from passing all the needed parameters into
the constructor, to numerous variations on the factory
pattern. And I wouldn''t be adverse to the other responder''s
practice of simply plugging values into the instance while
it''s still in the factory: most real world objects don''t contain
the logic for their construction, so why should our programming
objects?

John Roth



在星期三,2003-12-10 03:24,Graham Ashton写道:
On Wed, 2003-12-10 at 03:24, Graham Ashton wrote:
[snip]
我没有运行它,因此它可能有语法错误。它应该说明
原则;您可以直接使用属性,直到您在设置它们时采取
操作。然后,您可以将属性设为私有并且用一个访问真实属性的属性替换它,并在访问它时做任何你想要做的事情。
[snip] I''ve not run that so it may have syntax errors. It should illustrate the
principle though; you can use attributes directly until you want to take
actions when you set them. Then you can make the attribute "private" and
replace it with a property that access the real attribute, and does
whatever else you want to do when it''s accessed.




格雷厄姆的例子中有两点:


1.你必须从对象中继承才能使用属性。


2.当Graham将私有化称为私有时,他意味着您仍然可以直接访问属性变量

- 这是设计的。


参见示例以下。


干杯,


// m


#!/ usr / bin / env python


class鞋子:


def __init __(自我):

self._size =无

def getSize(self):

print" in getSize ..."

return self._size

def setSize(self,size):

print" in setSize ..."

self._size = size

size = property (getSize,setSize)


类Shoe2(对象):


def __ini t __(个体经营):

self._size =无

def getSize(个体经营):

print" in getSize ..."

返回self._size

def setSize(self,size):

print" in setSize ..."

self._size = size

size = property(getSize,setSize)


s =鞋子()

#由于我们没有从对象中继承,属性描述符

#不起作用;请注意,setSize()没有被调用...

s.size = 1

print s.size

#我们可以仍然访问私人成员变量。在Python中,

#private只是一个约定。

print s._size


#现在我们已经分组了从对象,我们的get / set方法是

#调用。

s2 = Shoe2()

s2.size = 1

print s2.size

#我们仍然可以访问私人会员变量。

print s2._size




A couple of points on Graham''s example:

1. You have to subclass from object in order to use property.

2. When Graham nominalizes private, he means that you can still access
the attribute variable directly--and that''s by design.

See example below.

Cheers,

// m

#!/usr/bin/env python

class Shoe:

def __init__(self):
self._size = None
def getSize(self):
print "In getSize..."
return self._size
def setSize(self, size):
print "In setSize..."
self._size = size
size = property(getSize, setSize)

class Shoe2(object):

def __init__(self):
self._size = None
def getSize(self):
print "In getSize..."
return self._size
def setSize(self, size):
print "In setSize..."
self._size = size
size = property(getSize, setSize)

s = Shoe()
# Since we haven''t subclassed from object, the property descriptor
# doesn''t work; notice that setSize() isn''t getting called...
s.size = 1
print s.size
# We can still access the "private" member variable. In Python,
# private is merely a convention.
print s._size

# Now that we''ve subclassed from object, our get/set methods are
# called.
s2 = Shoe2()
s2.size = 1
print s2.size
# And we can still access the "private" member variable.
print s2._size



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

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