[Python-ideas] aliasing
Greg Ewing
greg.ewing at canterbury.ac.nz
Thu Sep 1 00:52:11 CEST 2011
Steven D'Aprano wrote:
> Peio Borthelle wrote:
>
>>>>> a = 2
>>>>> b = alias("a")
>>>>> a = 'foo'
>>>>> b
>>
>> 'foo'
>
> I have often thought that would be a nice to have feature,
Seems to me it would be a confusing-to-have feature. By now
it's deeply embedded in Python programmers' brains that
assignment to a bare name can only change which object that
name refers to, and can't have any other side effects.
> You can always use one level of indirection:
>
> a = [2]
> b = a
> a[0] = 'foo' # Not a='foo'
> b[0]
> => print 'foo'
Also, if you're willing to use an object attribute rather
than a bare name, you can get the same effect using a
property.
class Switcheroo(object):
def get_b(self):
return self.a
def set_b(self, value):
self.a = value
b = property(get_b, set_b)
s = Switcheroo()
s.a = 17
print s.a
s.b = 42
print s.a
--
Greg
More information about the Python-ideas
mailing list