如何在Python中获取/设置类属性 [英] How to get/set class attributes in Python
问题描述
我从其他编程语言来到Python。我喜欢
隐藏一个类的所有属性,并且只通过方法提供对它们的访问权限
。其中一些语言允许我写一些与此类似的
int age()
{
返回theAge
}
void age(x:int)
{
theAge = x
}
(我通常在方法中做的比这更多)。我想在Python中做类似的事情,我想出了两种方法来做
它:第一个使用能力来使用变量
参数的数量......不是很好。另一个更好,并以这种方式使用
__setattr__和__getattr__:
class SuperClass:
def __setattr __(self,attrname ,值):
如果attrname ==''somevalue'':
self .__ dict __ [''something''] = value
否则:
引发AttributeError,attrname
def __str __(自我):
return str(self.something)
class Child(SuperClass):
def __setattr __(self,attrname,value):
如果attrname ==''funky'' :
self .__ dict __ [''fun''] = value
else:
SuperClass .__ setattr __(self,attrname,value)
def __str __(自我):
返回SuperClass .__ str __(自我)+'',''+ str(self.fun)
这是Pythonic吗?这样做的方式还是应该以不同的方式进行,或者我必须使用setX / getX(不寒而栗)
On Sun,2005年6月12日11:54:52 +0200,Kalle Anke< sk ***** @ gmail.com>写道:
我是从其他编程语言来到Python。我喜欢隐藏类的所有属性,并且只通过方法提供对它们的访问权限。其中一些语言允许我写一些与此类似的东西
int age()
{
返回年龄
}
void age(x:int)
{
theAge = x
}
(我通常在方法中做的比这更多)。我想在Python中做类似的事情,我想出了两种方法来做它:第一种方法使用能力来使用可变数量的
参数。 .. 不是很好。另一个更好,并以这种方式使用
__setattr__和__getattr__:
类SuperClass:
def __setattr __(self,attrname,value):
如果attrname == ''somevalue'':
self .__ dict __ [''something''] = value
else:
引发AttributeError,attrname
def __str __(self):
返回str(self.something)
类Child(SuperClass):
def __setattr __(self,attrname,value):
如果attrname ==''funky '':
self .__ dict __ [''fun''] = value
else:
SuperClass .__ setattr __(self,attrname,value)
def __str __( self):
返回SuperClass .__ str __(self)+'',''+ str(self.fun)
这是Pythonic吗?这样做的方式还是应该以不同的方式进行,或者我必须使用setX / getX(颤抖)
我完全是我自己也是Python的新手,但我的理解是
Kalle Anke写道:我是从其他编程语言来到Python。我喜欢隐藏类的所有属性,并且只通过方法提供对它们的访问权限。其中一些语言允许我写一些与此类似的东西
int age()
{
返回年龄
}
void age(x:int)
{
theAge = x
}
(我通常在方法中做的比这更多)。
你可以使用属性属性隐藏你的getsetters [1]:
< blockquote class =post_quotes> class person(object):
... def __init __(self):
... self.age = 0
... def set_age(自我,年龄):
...打印''设定%d''%年龄
...自我.__年龄=年龄
... def get_age(个体经营):
...打印''获取''
...返回自我.__年龄>
... age = property(get_age,set_age)
... joe = person()
set 0 joe.age = 20
set 20 print joe.age
get
20
[1] http://docs.python.org/lib/built-in-funcs.html
On Sun,2005年6月12日03:15:27 -0700,Steve Jorgensen< no **** @ nospam.nospam>
写道:
....这是Pythonic吗?这样做的方式或者我应该以不同的方式做到这一点,还是我必须使用setX / getX(颤抖)
我自己都是Python的新手,但我的理解是
....
哎呀 - 当我放松时,我以为我取消了那个帖子我什么都没说,
但是不知何故,它被张贴了。
I''m coming to Python from other programming languages. I like to
hide all attributes of a class and to only provide access to them
via methods. Some of these languages allows me to write something
similar to this
int age( )
{
return theAge
}
void age( x : int )
{
theAge = x
}
(I usually do more than this in the methods). I would like to do
something similar in Python, and I''ve come up with two ways to do
it: The first one uses the ability to use a variable number of
arguments ... not very nice. The other is better and uses
__setattr__ and __getattr__ in this way:
class SuperClass:
def __setattr__( self, attrname, value ):
if attrname == ''somevalue'':
self.__dict__[''something''] = value
else:
raise AttributeError, attrname
def __str__( self ):
return str(self.something)
class Child( SuperClass ):
def __setattr__( self, attrname, value ):
if attrname == ''funky'':
self.__dict__[''fun''] = value
else:
SuperClass.__setattr__( self, attrname, value )
def __str__( self ):
return SuperClass.__str__( self ) + '', '' + str(self.fun)
Is this the "Pythonic" way of doing it or should I do it in a different
way or do I have to use setX/getX (shudder)
On Sun, 12 Jun 2005 11:54:52 +0200, Kalle Anke <sk*****@gmail.com> wrote:
I''m coming to Python from other programming languages. I like to
hide all attributes of a class and to only provide access to them
via methods. Some of these languages allows me to write something
similar to this
int age( )
{
return theAge
}
void age( x : int )
{
theAge = x
}
(I usually do more than this in the methods). I would like to do
something similar in Python, and I''ve come up with two ways to do
it: The first one uses the ability to use a variable number of
arguments ... not very nice. The other is better and uses
__setattr__ and __getattr__ in this way:
class SuperClass:
def __setattr__( self, attrname, value ):
if attrname == ''somevalue'':
self.__dict__[''something''] = value
else:
raise AttributeError, attrname
def __str__( self ):
return str(self.something)
class Child( SuperClass ):
def __setattr__( self, attrname, value ):
if attrname == ''funky'':
self.__dict__[''fun''] = value
else:
SuperClass.__setattr__( self, attrname, value )
def __str__( self ):
return SuperClass.__str__( self ) + '', '' + str(self.fun)
Is this the "Pythonic" way of doing it or should I do it in a different
way or do I have to use setX/getX (shudder)
I''m totally new to Python myself, but my understanding is that
Kalle Anke wrote:I''m coming to Python from other programming languages. I like to
hide all attributes of a class and to only provide access to them
via methods. Some of these languages allows me to write something
similar to this
int age( )
{
return theAge
}
void age( x : int )
{
theAge = x
}
(I usually do more than this in the methods).
You can ''hide'' you getsetters using a property attribute[1]:
class person(object): ... def __init__(self):
... self.age = 0
... def set_age(self, age):
... print ''set %d'' % age
... self.__age = age
... def get_age(self):
... print ''get''
... return self.__age
... age = property(get_age, set_age)
... joe = person() set 0 joe.age = 20 set 20 print joe.age get
20
[1]http://docs.python.org/lib/built-in-funcs.html
On Sun, 12 Jun 2005 03:15:27 -0700, Steve Jorgensen <no****@nospam.nospam>
wrote:
....Is this the "Pythonic" way of doing it or should I do it in a different
way or do I have to use setX/getX (shudder)
I''m totally new to Python myself, but my understanding is that
....
Oops - I thought I cancelled that post when I relized I was saying nothing,
but somehow, it got posted.
这篇关于如何在Python中获取/设置类属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!