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 va

Re: lists of variables

2010-02-26 Thread Aahz
In article , Michael Pardee 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) <*> http://www.pythoncraft.com/ "Many customs in this life persist

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 ob

Re: lists of variables

2010-02-21 Thread bartc
"Michael Pardee" 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 only for literals? Nope

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 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 can't be pu

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. P

Re: lists of variables

2010-02-20 Thread Carl Banks
On Feb 20, 10:50 pm, 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? > > > But these > > references a

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, 7:25 pm, Michael Pardee 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 Jonathan Gardner
On Sat, Feb 20, 2010 at 7:25 PM, Michael Pardee 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 mylis

Re: lists of variables

2010-02-20 Thread Steven D'Aprano
On Sat, 20 Feb 2010 21:25:19 -0600, Michael Pardee wrote: > I'm relatively new to python and I was very surprised by the following > behavior: [snip] I don't see why. It's fairly unusual behaviour to want, and it would be surprising if you did this: def test(): x = 1 mylist = [2, 4, x]

Re: lists of variables

2010-02-20 Thread Ben Finney
Michael Pardee 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’. If that doesn't meet

Re: lists of variables

2010-02-20 Thread Stephen Hansen
On Sat, Feb 20, 2010 at 7:25 PM, Michael Pardee wrote: > 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 got data, and data obv

Re: lists of variables

2010-02-20 Thread Chris Rebert
On Sat, Feb 20, 2010 at 7:25 PM, Michael Pardee 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: >

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