Use list name as string

Vincent Davis vincent at vincentdavis.net
Thu Feb 5 04:37:04 CET 2009


Jervis Whitley wrote "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."
That is beyond my current knowledge. Any suggestions for reading about this?

Thanks
Vincent Davis



On Wed, Feb 4, 2009 at 8:24 PM, Jervis Whitley <jervisau at gmail.com> wrote:

> On Thu, Feb 5, 2009 at 3:57 AM, Vincent Davis <vincent at vincentdavis.net>
> 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])
>
> then
> def savedata(dataname):
>   # extract the first key(name) and value(data) from the dataname data
> type.
>   try:
>      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.
>
> Cheers,
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090204/eab5b45c/attachment.html>


More information about the Python-list mailing list