Use list name as string
Tim Chase
python.list at tim.thechases.com
Wed Feb 4 12:35:29 EST 2009
> I know nothing but that sucks. I can think of a lot of times I would like to
> do something similar. There really is no way to do this, it seems like there
> would be some simple way kind of like str(listname) but backwards or
> different.
Python does the only reasonable thing: doesn't give you access
to the "name". Consider the following situation:
a = [1,2,3,4,5]
b = a
savedata(b)
Do you want "a" or "b" as the variable-name? Both are valid
names for the same list.
If it matters, you can do something like this hack:
def savedata(**kwargs):
assert len(kwargs) == 1, "Just pass one parameter"
filename, data = kwargs.iteritems().next()
ext1 = '\.csv'
flex = filename + ext1
datawrite = csv.writer(open(flex, "wb"))
datawrite.writerows(data)
which can then be called with something like
savedata(foo=[1,2,3,4,5])
savedata(bar=a)
savedata(name=name)
to create "foo.csv" containing that data.
Or you could just pass it explicitly which would make more sense
and be easier to understand.
-tkc
More information about the Python-list
mailing list