Use list name as string

Jervis Whitley jervisau at
Wed Feb 4 22:24:48 EST 2009

On Thu, Feb 5, 2009 at 3:57 AM, Vincent Davis <vincent at> wrote:
> Sorry for not being clear
> I would have something like this
> x = [1, 2, 3,5 ,6 ,9,234]
> Then
> def savedata(dataname): ..........
> savedata(x)
> this would save a to a file called x.csv This is my problem, getting the
> name to be x.csv which is the same as the name of the list.
> and the data in the file would be
> 1,2,3,5,6,9,234 this parts works

It sounds like you would _really_ like to attach a name to this list of data.

How about this terrible example to solve your problem.

x = dict(x=[1,2,3,5,6,9,234])

def savedata(dataname):
   # extract the first key(name) and value(data) from the dataname data type.
      name, data = dataname.items()[0]
   except AttributeError:
      raise TypeError("Expecting a datatype that declares method 'items' :).")
   # we could catch other exceptions here but I wont (like empty items).

now you have your name and your data variable.
This way you don't declare your variable name when calling savedata
but when you
create your variable.

Although you should really solve your problem by thinking about it
from a completely
different angle, maybe subclassing your datatype and adding a 'name'
attribute ? I'm
sure some of the others here have suggested that already.


More information about the Python-list mailing list