alias for data member of class instance?

S

Sean McIlroy

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.xkrusty.x = 1
krusty.x
.... ? thanks.

peace
stm
 
L

Larry Bates

Sean McIlroy wrote:
Sean said:
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
krusty.x = 1
krusty.x

... ? 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
krusty.x = 1
krusty.x

... ? 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
 
B

Bruno Desthuilliers

Sean McIlroy a écrit :
hi all

is there a way to do this ...

class clown:
def __init__(self):
self.x = 0
self.y = ALIAS(self.x) ## FEASIBLE ?

class Clown(object):
def __init__(self):
self.x = 0

@apply
def x():
def fget(self):
return self._x
def fset(self, value):
self._x = value
return property(**locals())

y = x
 
B

Bruno Desthuilliers

Sean McIlroy a écrit :
hi all

is there a way to do this ...

class clown:
def __init__(self):
self.x = 0
self.y = ALIAS(self.x) ## FEASIBLE ?

class Alias(object):
def __init__(self, attrname):
self._attrname = attrname

def __get__(self, instance, cls):
if instance is None:
return self
return getattr(instance, self._attrname)

def __set__(self, instance, value):
setattr(instance, self._attrname, value)

class Clown2(object):
def __init__(self):
self.x = 0

y = Alias('x')
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top