lists of variables
apt.shansen at gmail.com
Sun Feb 21 04:58:10 CET 2010
On Sat, Feb 20, 2010 at 7:25 PM, Michael Pardee
<python-list at open-sense.com>wrote:
> But what would be "the python way" to accomplish "list of variables"
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.
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.
a = 1
In many languages, that's making a variable "a" and assigning it the value
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.
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
There's no way to make a "list of variables", because /variables/ are the
part that's really missing. If you do:
lst = [a, b]
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.
So, long story short: what's "the python way" to accomplish a "list of
variables"? Well... IMHO, it's to not do so. : )
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
*: Yes, I know about local namespaces being optimized in CPython to be an
array-like structure. Implementation detail.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-list