alias for data member of class instance?
Larry Bates
larry.bates at websafe.com
Mon Feb 5 16:12:45 EST 2007
Sean McIlroy wrote:
Sean McIlroy wrote:
> hi all
>
> is there a way to do this ...
>
> class clown:
> def __init__(self):
> self.x = 0
> self.y = ALIAS(self.x) ## FEASIBLE ?
>
> ... so that you get results like this ...
>
> krusty = clown()
> krusty.x
>>> 0
> krusty.y
>>> 0
> krusty.x = 1
> krusty.x
>>> 1
> krusty.y
>>> 1
>
> ... ? thanks.
>
> peace
> stm
>
> hi all
>
> is there a way to do this ...
>
> class clown:
> def __init__(self):
> self.x = 0
> self.y = ALIAS(self.x) ## FEASIBLE ?
>
> ... so that you get results like this ...
>
> krusty = clown()
> krusty.x
>>> 0
> krusty.y
>>> 0
> krusty.x = 1
> krusty.x
>>> 1
> krusty.y
>>> 1
>
> ... ? thanks.
>
> peace
> stm
>
Not sure why you want it, but here is one solution:
class clown:
def __init__(self):
self.x=0
def __getattr__(self, key):
if key == 'y': return self.x
return self.__dict__[key]
-Larry
More information about the Python-list
mailing list