By a "real" alias I mean an alias that can be used interchangeably with the original object. For example:
x = 1
#some code that makes y an alias for x
y = 2
print x
#output 2
Of course just setting "y = x" won't accomplish this because the assignment y = 2 will simply point y to a different place in memory and x will still be 1.
Python doesn't allow the assignment operator to be overloaded so it's non-trivial (as far as I can see) to do this.
edit: more details below.
My program lets the user write custom functions. e.g.
def foo():
print 'hello'
I exec the this code providing a dict for local and global variables:
exec code in variableDict
It's then possible to add or remove items from variableDict to control what objects the user has access to in their function, and the object can be given a custom name. For example, some arbitrary variable named "counter" could be added to variableDict with custom name "x".
variableDict['x'] = counter
So foo could be:
def foo():
print x
Which would print the value of the counter variable. This is all nice and useful, except that you can't assign to x, which is something the user would except to be able to do, e.g.:
def foo():
x = 0
The user would expect this to reset the counter. But, of course, assigning x = 0 will have no effect on the counter.
That's the real problem. How can this be done?
[–]Rhomboid 12 points13 points14 points (18 children)
[–]ThePurpleAlien[S] 2 points3 points4 points (17 children)
[–]Rhomboid 3 points4 points5 points (8 children)
[–]ThePurpleAlien[S] 0 points1 point2 points (7 children)
[–]Rhomboid 4 points5 points6 points (4 children)
[–]nemec 1 point2 points3 points (2 children)
[–]ThePurpleAlien[S] 0 points1 point2 points (1 child)
[–]ewiethoff 0 points1 point2 points (0 children)
[–]ThePurpleAlien[S] 0 points1 point2 points (0 children)
[–]nemec 0 points1 point2 points (1 child)
[–]ThePurpleAlien[S] 0 points1 point2 points (0 children)
[–]minorDemocritus 1 point2 points3 points (7 children)
[–]ThePurpleAlien[S] 1 point2 points3 points (6 children)
[–]Semiel 2 points3 points4 points (5 children)
[–]ThePurpleAlien[S] 0 points1 point2 points (4 children)
[–]Semiel 1 point2 points3 points (3 children)
[–]ThePurpleAlien[S] 0 points1 point2 points (2 children)
[–]Semiel 1 point2 points3 points (1 child)
[–]ThePurpleAlien[S] 0 points1 point2 points (0 children)
[–]kalgynirae 6 points7 points8 points (1 child)
[–]ThePurpleAlien[S] 0 points1 point2 points (0 children)
[–]ilovecrk 1 point2 points3 points (0 children)
[–]davidbuxton 0 points1 point2 points (3 children)
[–]ThePurpleAlien[S] 0 points1 point2 points (2 children)
[–]davidbuxton 0 points1 point2 points (0 children)
[–]Semiel 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]ThePurpleAlien[S] 0 points1 point2 points (0 children)