Use list name as string

Rhodri James rhodri at wildebst.demon.co.uk
Thu Feb 5 03:55:04 CET 2009


On Thu, 05 Feb 2009 01:36:17 -0000, Vincent Davis  
<vincent at vincentdavis.net> wrote:

> if I start with
> M = [1,3,5,7]
> M is [1,3,5,7]
> This seems one way, as [1,3,5,7] is not M in the sense that there is
> no operation I can preform on [1,3,5,7] and get M back. Other than
> asking/testing M==[1,3,5,7]

Correct.  If you actually try that, you get this:

Python 2.5.2 (r252:60911, May  7 2008, 15:21:12)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> M = [1,3,5,7]
>>> M is [1,3,5,7]
False

This isn't just me being funny with your suggested syntax, it's
making an important point: the second [1,3,5,7] is an entirely
different object to the list we named M, it just happens to
contain the same data.

> This seems fine to me. but when I savedata(M) it seems I should be
> able to refer to both [1,3,5,7] and "M", "I mean I did just type it
> why type it again)
> My argument comes down to; we use M so we don't have to type
> [1,3,5,7], I realize that this is in part because we might not no what
> M will be.

That's an oversimplification, and you're battering away well past the
point where it works.  In any language, not just Python.

> This is starting to sound like double talk on my part, I have only
> been programing in python for 2 weeks so my credibility is only that
> of an outside that MAY have a reasonable way of thinking of this or at
> least a feature I would like.

The problem is you seem to be thinking in terms of objects having names.
They don't.  Names have objects.

What I mean by that is that when you type "M = [1,3,5,7]", you are saying
"when I say 'M', substitute in this list (not just one that looks like it,
this exact list".  When you type "N = M", similarly you get the name N
bound to the same exact list; if you then type "N[1] = 2" and print out
M, it'll show up as [1,2,5,7] -- it's still the same list under both names.
In effect (to oversimplify more than a bit), Python looks up the name
in a dictionary to get the object it represents.

The reverse lookup doesn't exist, largely because it isn't unique.  Our
[1,3,5,7] list has no idea what names, if any, refer to it, and in all
but the most trivial cases the information isn't really meaningful.
Suppose that we did have a "getvariablename" function, what would you
expect the following code to do?

def f(thing):
     print getvariablename(thing)

m = [1,3,5,7]
for n in m:
     f(n)


Should it say "n" for all of them?  Or "['n', 'thing']", since 'thing'
is after all a perfectly valid name for it?

Then what about this:

for i in range(len(m)):
     f(m[i])


Should it say "m[i]" for all of them, or "m[0]" "m[1]" and so on, or
what?

> by the way what is "**kwargs" I can't find any documentation on this?

http://docs.python.org/tutorial/controlflow.html#keyword-arguments

In a function definition, "**name" mops up all the keyword arguments
to the function that aren't explicit formal parameters into a
dictionary called "name", indexed by keyword.  "kwargs" seems to be
the traditional name to use.  So:

>>> def demo(action, **kwargs):
...   print kwargs
...
>>> demo(action="dummy", text="wombats are go")
{'text': 'wombats are go'}


You can also use it the other way round to expand a dictionary into
keyword parameters to a function call:

>>> myargs = { 'action': 'avoid', 'where': 'Camelot', 'because': 'it is a  
>>> silly place' }
>>> demo(**myargs)
{'because': 'it is a silly place', 'where': 'Camelot'}

-- 
Rhodri James *-* Wildebeeste Herder to the Masses



More information about the Python-list mailing list