469,271 Members | 1,787 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

alias for data member of class instance?

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

Feb 5 '07 #1
4 1402
"Sean McIlroy" <se**********@yahoo.comwrites:
self.y = ALIAS(self.x) ## FEASIBLE ?
The closest thing is probably to use @property.
Feb 5 '07 #2
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
Feb 5 '07 #3
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
Feb 5 '07 #4
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')

Feb 5 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Nobody You Know | last post: by
3 posts views Thread by Daniel Graifer | last post: by
28 posts views Thread by Act | last post: by
10 posts views Thread by Zap | last post: by
7 posts views Thread by The|Godfather | last post: by
4 posts views Thread by aaragon | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.