
Hi, First, thank you to all the development community for this fabulous language (I'm french so my english is a bit...bad and basic). I'm quite new to programming and python is my first language. As beginner I had problems to deal with all aliasing stuff (in-place changes or not...). So my suggestion is perhaps not to change this (altough I find it a bit opposite to the python sense...) but to have a real aliasing fonction: -----------------------------
b is always a, it doesn't point to the same data, it points to the pointer a itself ! The arg is a string because otherwise the interpreter would give the value as arg, not the pointer. It could also be more complexe: -----------------------------
Here I alias an expression (that's also why the arg must be a string). But I don't know if this last example is something good because a would have to stay a number (or generate an exception by calling b). If a is destroyed, then calling b would generate an exception...I don't have enough experience in python programming to really know how work the garbage collector, so I don't know if a could be destroyed by it. This is the end of my suggestion (I hope it wasn't already proposed, else...). Amicalement, Peio

Le 30 août 2011 à 15:51, Herman Sheremetyev a écrit :
Great, I hadn't tought to the lambda fonction, and it is a solution... but it isn't quite what I proposed: here b is a fonction, you have to call it to have the number. If you want to use it in your code, e.g. two int variable must stay to the same value, you can't give a fonction as argument, and what I want to avoid is precisely to change both value one by one. I misspoke something in my last message (and it wasn't also quite clear in my mind), I sould have written: --------------------------------
a = 3 b = Alias("a") # Alias is a class (or data type?).
Where b is an Alias instance. I don't know anything in C language, so I don't know how works data types defining, but I imagine the Alias class as a normal variable wich's value changes dynamically. Is it technically possible ? Amicalement, Peio

On Tue, Aug 30, 2011 at 11:20 PM, Peio Borthelle <peio.borthelle@gmail.com> wrote:
The fact that identifiers are just references to objects rather than objects in their own right is central to the way the language works (in a very real sense, everything of significance in a Python program is either an object or a reference to an object). The distinction between mutable objects (e.g. lists) and immutable objects (e.g. tuples) is an immediate outcome of that - since the latter can't be modified directly, the only thing you can do is change which one you're referring to, which doesn't affect any other references to the old value. If you want mutable state, you have to store it on a mutable object. Regards, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia

Peio Borthelle wrote:
I have often thought that would be a nice to have feature, but to be honest, I have never found a use for it that was absolutely necessary. I have always found another way to solve the problem. You can always use one level of indirection: a = [2] b = a a[0] = 'foo' # Not a='foo' b[0] => print 'foo' To make it work using just ordinary assignment syntax, as you suggest, requires more than just an "alias" function. It would need changes to the Python internals. Possibly very large changes. Without a convincing use-case, I don't think that will happen. So even though I think this would be a neat feature to have, and possibly even useful, I don't think it is useful enough to justify the work needed to make it happen. -- Steven

On Wed, Aug 31, 2011 at 5:41 AM, Steven D'Aprano <steve@pearwood.info>wrote:
Well, once you clear up some of the ambiguities in the request, it's not quite so bad. class F(object): def m(self): alias(a, b) Which name space is a being aliased in? Function local? Instance? Class? Module? So alias needs to take a namespace of some sort. The appropriate object is easy to find, and should do the trick (though function local might be entertaining). For simplicity, we'll assume that aliases have to be in the same namespace (though doing otherwise isn't really harder, just slower). Passing in variable values doesn't work very well - especially for the new alias, which may not *have* a value yet! For the name to be aliased, you could in theory look up the value in the namespace and use the corresponding name, but there may be more than one of those. So pass that one in as a string as well. So make the call "alias(o, name1, name2)". o is a module, class, instance, or function. Name lookups are done via the __dict__ dict for all of those. It's easy to write a dict subclass that aliases it's entries. So the internal changes would be making __dict__ a writable attribute on any objects which it currently isn't, and making sure that the various objects use __setitem__ to change attribute values. I suspect the performance hit - even if you don't use this feature - would be noticeable. If you used it, it would be even worse. -1. <mike

Steven D'Aprano wrote:
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.
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

Le 30 août 2011 à 15:51, Herman Sheremetyev a écrit :
Great, I hadn't tought to the lambda fonction, and it is a solution... but it isn't quite what I proposed: here b is a fonction, you have to call it to have the number. If you want to use it in your code, e.g. two int variable must stay to the same value, you can't give a fonction as argument, and what I want to avoid is precisely to change both value one by one. I misspoke something in my last message (and it wasn't also quite clear in my mind), I sould have written: --------------------------------
a = 3 b = Alias("a") # Alias is a class (or data type?).
Where b is an Alias instance. I don't know anything in C language, so I don't know how works data types defining, but I imagine the Alias class as a normal variable wich's value changes dynamically. Is it technically possible ? Amicalement, Peio

On Tue, Aug 30, 2011 at 11:20 PM, Peio Borthelle <peio.borthelle@gmail.com> wrote:
The fact that identifiers are just references to objects rather than objects in their own right is central to the way the language works (in a very real sense, everything of significance in a Python program is either an object or a reference to an object). The distinction between mutable objects (e.g. lists) and immutable objects (e.g. tuples) is an immediate outcome of that - since the latter can't be modified directly, the only thing you can do is change which one you're referring to, which doesn't affect any other references to the old value. If you want mutable state, you have to store it on a mutable object. Regards, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia

Peio Borthelle wrote:
I have often thought that would be a nice to have feature, but to be honest, I have never found a use for it that was absolutely necessary. I have always found another way to solve the problem. You can always use one level of indirection: a = [2] b = a a[0] = 'foo' # Not a='foo' b[0] => print 'foo' To make it work using just ordinary assignment syntax, as you suggest, requires more than just an "alias" function. It would need changes to the Python internals. Possibly very large changes. Without a convincing use-case, I don't think that will happen. So even though I think this would be a neat feature to have, and possibly even useful, I don't think it is useful enough to justify the work needed to make it happen. -- Steven

On Wed, Aug 31, 2011 at 5:41 AM, Steven D'Aprano <steve@pearwood.info>wrote:
Well, once you clear up some of the ambiguities in the request, it's not quite so bad. class F(object): def m(self): alias(a, b) Which name space is a being aliased in? Function local? Instance? Class? Module? So alias needs to take a namespace of some sort. The appropriate object is easy to find, and should do the trick (though function local might be entertaining). For simplicity, we'll assume that aliases have to be in the same namespace (though doing otherwise isn't really harder, just slower). Passing in variable values doesn't work very well - especially for the new alias, which may not *have* a value yet! For the name to be aliased, you could in theory look up the value in the namespace and use the corresponding name, but there may be more than one of those. So pass that one in as a string as well. So make the call "alias(o, name1, name2)". o is a module, class, instance, or function. Name lookups are done via the __dict__ dict for all of those. It's easy to write a dict subclass that aliases it's entries. So the internal changes would be making __dict__ a writable attribute on any objects which it currently isn't, and making sure that the various objects use __setitem__ to change attribute values. I suspect the performance hit - even if you don't use this feature - would be noticeable. If you used it, it would be even worse. -1. <mike

Steven D'Aprano wrote:
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.
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
participants (6)
-
Greg Ewing
-
Herman Sheremetyev
-
Mike Meyer
-
Nick Coghlan
-
Peio Borthelle
-
Steven D'Aprano