Use list name as string

Tino Wildenhain tino at wildenhain.de
Thu Feb 5 04:13:24 EST 2009


Hi,

Vincent Davis 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

the problem you are facing comes from a little misunderstanding.
To clarify:

python objects are nameless.

You can bind them to any number of names (aka variables)

 >>> 1 # unnamed integer object with value 1
1

 >>> a=1 # bind the integer object to name 'a'
 >>> b=a # bind the same integer object referred to by name a to name b

therefore in your above example, which "name" should your
savedata pick up for the filename? the 'x' of the first
assignment or the 'dataname' of the assignment in the function
call?

The only solution I see would be to add a property to your datastore
to give it its own unique name. (By subclassing and providing
a name attribute or property) - and while you are at it, maybe
you want to put the 'write to file' part into the class as well.

Regards
Tino

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 3241 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20090205/2f0c1380/attachment.bin>


More information about the Python-list mailing list