On Sat, Feb 20, 2010 at 7:25 PM, Michael Pardee <span dir="ltr"><<a href="mailto:python-list@open-sense.com">python-list@open-sense.com</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

But what would be "the python way" to accomplish "list of variables"<br>
functionality?<br></blockquote><div><br></div><div>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 obviously has to beĀ identifiableĀ in some way, but they aren't put together like "variables" are. A variable is a box with a name on it, and in that context a 'list of variables' makes sense. You're moving around the box, or referencing the box itself. It may happen to have something in it, but that 'something' doesn't really have an identity of its own. Its just.. the thing that's in box-A.</div>

<div><br></div><div>In Python, everything's an object. And objects don't really have identities of their own(okay, they do, but its not meaningful except to an identity test), but they can be named. They object has no flabbering clue what its named, or how many different people know its name, but hey.</div>

<div><br></div><div>a = 1</div><div><br></div><div>In many languages, that's making a variable "a" and assigning it the value of 1.</div><div><br></div><div>In Python, that's making an int object with a value of 1, and giving it a name in the current namespace. This -isn't- just using dumber/simpler words to say the same thing :) All(*) python namespaces are really dictionaries behind the scenes. a = 1 is actually doing, current_namespaces["a"] = 1.</div>

<div><br></div><div>Those may sound similar, but they're really not: most importantly, because the "a" in another language is a -thing- unto itself which may hold an object. But "a" in Python is just a label in some dictionary somewhere. You can get the object a is naming, but you can't get /a/ itself. Its not a 'thing'.</div>

<div><br></div><div>There's no way to make a "list of variables", because /variables/ are the part that's really missing. If you do:</div><div><br></div><div>lst = [a, b]</div><div><br></div><div>Python doesn't create a list and put two variables in it. It creates a list, and while doing so, sees that we want to pass 'a' and 'b' into that list. So it grabs the actual objects a and b are naming, and puts them in the list.</div>

<div><br></div><div>So, long story short: what's "the python way" to accomplish a "list of variables"? Well... IMHO, it's to not do so. : )</div><div><br></div><div>Make a dict, pass the dict around, there's your list of variables. True, you can't ever access them /as/ variables, and have to access them as dict items, but... that's the Python way to do that. If order matters, do an ordered dict.</div>

<div><br></div><div>HTH,</div><div>--S</div><div><br></div><div>*: Yes, I know about local namespaces being optimized in CPython to be an array-like structure. Implementation detail.</div></div>