473,385 Members | 1,901 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 1533
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Nobody You Know | last post by:
I need a member variable in class A known within an instance of class B, even though B does not instantiate A. My solution was to make the member variable private static, and create a public...
3
by: Daniel Graifer | last post by:
Why doesn't c++ support virtual data? It supports class data of type pointer to function (that's what virtual functions really are). In terms of implementation, why can't I have other types of...
28
by: Act | last post by:
Why is it suggested to not define data members as "protected"? Thanks for help!
10
by: Zap | last post by:
Widespread opinion is that public data members are evil, because if you have to change the way the data is stored in your class you have to break the code accessing it, etc. After reading this...
6
by: lovecreatesbeauty | last post by:
Hello Experts, Why static data members can be declared as the type of class which it belongs to? Inside a class, non-static data members such as pointers and references can be declared as...
7
by: The|Godfather | last post by:
Hi everybody, I read Scotte Meyer's "Effective C++" book twice and I know that he mentioned something specific about constructors and destructors that was related to the following...
4
by: aaragon | last post by:
Hi everyone, I wrote some code for a class but then I decided that I wanted to inherit from this class. Well, it turns out that now I have to put "this->" in front of all the member variables of...
8
by: weird0 | last post by:
The scenario is that i have two forms, Form1( main form) and Form2 and both of them opened. Form1 has called Form2 inside of its constructor. class Form1 { const int NodeLimit=30; Node...
162
by: Sh4wn | last post by:
Hi, first, python is one of my fav languages, and i'll definitely keep developing with it. But, there's 1 one thing what I -really- miss: data hiding. I know member vars are private when you...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.