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.