Re: lists of variables

2010-02-26 Thread Aahz
In article mailman.22.1266722722.4577.python-l...@python.org, Michael Pardee python-l...@open-sense.com wrote: I'm relatively new to python and I was very surprised by the following behavior: http://starship.python.net/crew/mwh/hacks/objectthink.html -- Aahz (a...@pythoncraft.com) *

Re: lists of variables

2010-02-26 Thread Alf P. Steinbach
* Michael Pardee: I'm relatively new to python and I was very surprised by the following behavior: a=1 b=2 'a' refers to an object representing the integer 1. Since 1 is an immutable value you can just as well think of it as 'a' containing the value 1, because a reference to an immutable

Re: lists of variables

2010-02-21 Thread Lie Ryan
On 02/21/10 15:21, Steven D'Aprano wrote: So it looks like variables in a list are stored as object references. Python doesn't store variables in lists, it stores objects, always. Even Python variables aren't variables *grin*, although it's really difficult to avoid using the term. Python

Re: lists of variables

2010-02-21 Thread Steven D'Aprano
On Sat, 20 Feb 2010 23:44:29 -0800, Carl Banks wrote: On Feb 20, 10:50 pm, Steven D'Aprano st...@remove-this- cybersource.com.au wrote: What makes you say that? [...] I don't even understand this. [...] I'm just confused why you think that lexical scoping is equivalent to references that

Re: lists of variables

2010-02-21 Thread bartc
Michael Pardee python-l...@open-sense.com wrote in message news:mailman.22.1266722722.4577.python-l...@python.org... I'm relatively new to python and I was very surprised by the following behavior: a=1 b=2 mylist=[a,b] print mylist [1, 2] a=3 print mylist [1, 2] Whoah! Are python lists

Re: lists of variables

2010-02-21 Thread Gregory Ewing
Steven D'Aprano wrote: On Sat, 20 Feb 2010 22:31:44 -0800, Carl Banks wrote: The one place where Python does have references is when accessing variables in an enclosing scope (not counting module-level). What makes you say that? I think Carl is talking about cells, which *are* actually

lists of variables

2010-02-20 Thread Michael Pardee
I'm relatively new to python and I was very surprised by the following behavior: a=1 b=2 mylist=[a,b] print mylist [1, 2] a=3 print mylist [1, 2] Whoah! Are python lists only for literals? Nope: c={} d={} mydlist=[c,d] print mydlist [{}, {}] c['x']=1 print mydlist [{'x': 1}, {}]

Re: lists of variables

2010-02-20 Thread Chris Rebert
On Sat, Feb 20, 2010 at 7:25 PM, Michael Pardee python-l...@open-sense.com wrote: I'm relatively new to python and I was very surprised by the following behavior: a=1 b=2 mylist=[a,b] print mylist [1, 2] a=3 print mylist [1, 2] Whoah!  Are python lists only for literals?  Nope:

Re: lists of variables

2010-02-20 Thread Stephen Hansen
On Sat, Feb 20, 2010 at 7:25 PM, Michael Pardee python-l...@open-sense.comwrote: But what would be the python way to accomplish list of variables functionality? The problem is... Python doesn't have variables. At least not in the way that you may be used to from other languages. Yeah, it's

Re: lists of variables

2010-02-20 Thread Ben Finney
Michael Pardee python-l...@open-sense.com writes: But what would be the python way to accomplish list of variables functionality? You'll need to explain what “list of variables” functionality is. If you mean “collection of name-to-value mappings”, the native mapping type in Python is ‘dict’.

Re: lists of variables

2010-02-20 Thread Steven D'Aprano
] function(mylist) assert x == 1 and the assertion failed, even though you never passed x to the function. Such behaviour could easily turn into a never-ending source of bugs. So it looks like variables in a list are stored as object references. Python doesn't store variables in lists

Re: lists of variables

2010-02-20 Thread Jonathan Gardner
On Sat, Feb 20, 2010 at 7:25 PM, Michael Pardee python-l...@open-sense.com wrote: But what would be the python way to accomplish list of variables functionality? You're looking for namespaces, AKA dicts. vars = {} vars['a'] = 1 vars['b'] = 2 mylist = ['a', 'b'] print [vars[i] for i in

Re: lists of variables

2010-02-20 Thread Carl Banks
On Feb 20, 7:25 pm, Michael Pardee python-l...@open-sense.com wrote: I'm relatively new to python and I was very surprised by the following behavior: a=1 b=2 mylist=[a,b] print mylist [1, 2] a=3 print mylist [1, 2] Whoah!  Are python lists only for literals?  Nope: c={}

Re: lists of variables

2010-02-20 Thread Steven D'Aprano
On Sat, 20 Feb 2010 22:31:44 -0800, Carl Banks wrote: The one place where Python does have references is when accessing variables in an enclosing scope (not counting module-level). What makes you say that? But these references aren't objects, so you can't store them in a list, so it

Re: lists of variables

2010-02-20 Thread Carl Banks
On Feb 20, 10:50 pm, Steven D'Aprano st...@remove-this- cybersource.com.au wrote: On Sat, 20 Feb 2010 22:31:44 -0800, Carl Banks wrote: The one place where Python does have references is when accessing variables in an enclosing scope (not counting module-level).   What makes you say that?