python中如何获取类的属性,如何在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 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

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值