Re: Use list name as string
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 smime.p7s Description: S/MIME Cryptographic Signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Use list name as string
Hendrik van Rooyen wrote: MRAB goo...@mrett.plus.com wrote: The actual names of the variables and functions shouldn't matter to the outside world; the name of an output file shouldn't depend on the name of a variable. That is a matter of opinion. It is however, an interesting problem, namely: How does one get hold of the actual name by which some parameter is passed? you may want to print, as a debug thingy: print the name passed in was: , ImpossibleThingYieldingName print and it evaluates to: , ArgumentPassed This is possible to some degree: import inspect def F(a): frame_obj,filename,line_no, func_name,contextlines, contextindex=(inspect.getouterframes(inspect.currentframe()))[1] print F(%s) called from '%s' within '%s' line %d % (repr(a),filename,func_name,line_no) for ln,srcline in enumerate(contextlines or []): print %3s : %s % ('*' if ln==contextindex else '',srcline) just play around calling the above function from different places and you should see what I mean :-) Regards Tino smime.p7s Description: S/MIME Cryptographic Signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Use list name as string
On Thu, 05 Feb 2009 03:32:59 -, Vincent Davis vinc...@vincentdavis.net wrote: The problem is you seem to be thinking in terms of objects having names. They don't. Names have objects.I agree this is my problem. This is not correct terminology then? The name of the object is anobject No. The name of the object is a name. It doesn't really exist as an object at all. As others have said, if you really want this information you'll need to write your own class with a name attribute, and assign a suitable string to it. -- Rhodri James *-* Wildebeeste Herder to the Masses -- http://mail.python.org/mailman/listinfo/python-list
Use list name as string
Do to laking knowledge my google searches have not turned up an answer for me. I know this is wrong it uses the items in the list as the filename, how do I refer to the dataname and not the items in it. def savedata(dataname): filename = str(dataname) # this does not do what I would like it to ext1 = '\.csv' flex = filename + ext1 datawrite = csv.writer(open(flex, wb)) datawrite.writerows(dataname) Thanks Vincent Davis -- http://mail.python.org/mailman/listinfo/python-list
Re: Use list name as string
Vincent Davis wrote: Do to laking knowledge my google searches have not turned up an answer for me. I know this is wrong it uses the items in the list as the filename, how do I refer to the dataname and not the items in it. def savedata(dataname): filename = str(dataname) # this does not do what I would like it to ext1 = '\.csv' flex = filename + ext1 datawrite = csv.writer(open(flex, wb)) datawrite.writerows(dataname) I need more information to answer this question -- because I have no idea what the question means. SO: What is dataname? What would you like filename to be after that line. One example of dataname and the desired filename is probably enough to answer your question. Thanks Vincent Davis -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Use list name as string
I know this is wrong it uses the items in the list as the filename, how do I refer to the dataname and not the items in it. Without a sample value for dataname, it's hard to tell what you're trying to do. Do you mean dataname = ['path', 'to', 'file'] ... filename = os.sep.join(dataname) Or you have a list of one value, and just want its first item? filename = dataname[0] def savedata(dataname): filename = str(dataname) # this does not do what I would like it to It would help if you detailed what you *would* like it to do... from MindReading import answer Traceback (most recent call last): File stdin, line 6, in comp.lang.python ImportError: No module named MindReading Similar failure attempting import dwim -tkc -- http://mail.python.org/mailman/listinfo/python-list
Re: Use list name as string
Sorry for not being clearI 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 Thanks Vincent Davis 720-301-3003 On Wed, Feb 4, 2009 at 9:48 AM, Tim Chase python.l...@tim.thechases.comwrote: I know this is wrong it uses the items in the list as the filename, how do I refer to the dataname and not the items in it. Without a sample value for dataname, it's hard to tell what you're trying to do. Do you mean dataname = ['path', 'to', 'file'] ... filename = os.sep.join(dataname) Or you have a list of one value, and just want its first item? filename = dataname[0] def savedata(dataname): filename = str(dataname) # this does not do what I would like it to It would help if you detailed what you *would* like it to do... from MindReading import answer Traceback (most recent call last): File stdin, line 6, in comp.lang.python ImportError: No module named MindReading Similar failure attempting import dwim -tkc -- http://mail.python.org/mailman/listinfo/python-list
Re: Use list name as string
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 list itself doesn't have a name. You need to pass in both the name and the list: def savedata(name, data): .. savedata(x, x) -- http://mail.python.org/mailman/listinfo/python-list
Re: Use list name as string
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. Thanks Vincent Davis On Wed, Feb 4, 2009 at 10:07 AM, MRAB goo...@mrabarnett.plus.com wrote: 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 list itself doesn't have a name. You need to pass in both the name and the list: def savedata(name, data): .. savedata(x, x) -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Use list name as string
I guess what I am saying is that it does not seem like I am adding any information that is not already there when I have to enter that list and list name after all they are the same. Thanks Vincent Davis On Wed, Feb 4, 2009 at 10:18 AM, Vincent Davis vinc...@vincentdavis.netwrote: 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. Thanks Vincent Davis On Wed, Feb 4, 2009 at 10:07 AM, MRAB goo...@mrabarnett.plus.com wrote: 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 list itself doesn't have a name. You need to pass in both the name and the list: def savedata(name, data): .. savedata(x, x) -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Use list name as string
can I do it the otherway, that issavedata('nameoflist') Thanks Vincent Davis 720-301-3003 On Wed, Feb 4, 2009 at 10:23 AM, Vincent Davis vinc...@vincentdavis.netwrote: I guess what I am saying is that it does not seem like I am adding any information that is not already there when I have to enter that list and list name after all they are the same. Thanks Vincent Davis On Wed, Feb 4, 2009 at 10:18 AM, Vincent Davis vinc...@vincentdavis.netwrote: 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. Thanks Vincent Davis On Wed, Feb 4, 2009 at 10:07 AM, MRAB goo...@mrabarnett.plus.com wrote: 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 list itself doesn't have a name. You need to pass in both the name and the list: def savedata(name, data): .. savedata(x, x) -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Use list name as string
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Use list name as string
can I do it the otherway, that issavedata('nameoflist') for limited cases where your variable is defined globally, you can use: a = [1,2,3,4] def save(s): ... print globals().get(s, UNDEFINED) ... save(a) [1, 2, 3, 4] save(b) UNDEFINED b = (6,5,4,3) save(b) (6, 5, 4, 3) However, it's a hideous hack, and fragile as demonstrated by x = 7000 def baz(): ... x = (7,8,9) # this isn't in save()'s globals() ... save(x) ... baz() 7000 x = 8000 baz() 8000 and using locals() doesn't help either: print locals().get(s, globals().get(s, UNDEFINED)) and has even weirder (but totally understandable) behavior: save(s) # s hasn't been defined in globals() 's' Just pass the filename as a string, and skip trying to sniff internal variable-names. Or you'll experience a world of headaches. -tkc -- http://mail.python.org/mailman/listinfo/python-list
Re: Use list name as string
Vincent Davis wrote: I guess what I am saying is that it does not seem like I am adding any information that is not already there when I have to enter that list and list name after all they are the same. If you write: y = x then both x and y refer to the same list. The actual names of the variables and functions shouldn't matter to the outside world; the name of an output file shouldn't depend on the name of a variable. On Wed, Feb 4, 2009 at 10:18 AM, Vincent Davis vinc...@vincentdavis.net mailto:vinc...@vincentdavis.net wrote: 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, itseems like there would be some simple way kind of like str(listname)but backwards or different. On Wed, Feb 4, 2009 at 10:07 AM, MRAB goo...@mrabarnett.plus.com mailto:goo...@mrabarnett.plus.com wrote: 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 list itself doesn't have a name. You need to pass in both the name and the list: def savedata(name, data): .. savedata(x, x) -- http://mail.python.org/mailman/listinfo/python-list
Re: Use list name as string
On Wed, 04 Feb 2009 17:23:55 -, Vincent Davis vinc...@vincentdavis.net wrote: I guess what I am saying is that it does not seem like I am adding any information that is not already there when I have to enter that list and list name after all they are the same. Thanks But you are. Consider just for a moment what happens when you execute savedata([1, 2, 3, 55]). Fundamentally, the concept of a single unique name for any object isn't something built into the language (or, indeed, most languages I can think of). An object can have no names (though it'll promptly get garbage collected if it isn't assigned to a name somehow), or just as easily one or many names. -- Rhodri James *-* Wildebeeste Herder to the Masses -- http://mail.python.org/mailman/listinfo/python-list
Re: Use list name as string
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] 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. 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. Thanks for the comments by the way what is **kwargs I can't find any documentation on this? Thanks Vincent Davis On Wed, Feb 4, 2009 at 5:09 PM, Rhodri James rho...@wildebst.demon.co.uk wrote: On Wed, 04 Feb 2009 17:23:55 -, Vincent Davis vinc...@vincentdavis.net wrote: I guess what I am saying is that it does not seem like I am adding any information that is not already there when I have to enter that list and list name after all they are the same. Thanks But you are. Consider just for a moment what happens when you execute savedata([1, 2, 3, 55]). Fundamentally, the concept of a single unique name for any object isn't something built into the language (or, indeed, most languages I can think of). An object can have no names (though it'll promptly get garbage collected if it isn't assigned to a name somehow), or just as easily one or many names. -- Rhodri James *-* Wildebeeste Herder to the Masses -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Use list name as string
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. 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 (as pointed out elsewhere on the list), an argument to a function may have zero or more names at the time the function is called. There *is* *no* canonical name for an object. If you want to name an object, pass it as a function-argument. Or use its ID. However, since Python has the flexibility to handle any of the following: a = [1,2,3] b = [5,6,7] c = b foo([8,9,10]) foo(a) foo(b) foo(c) While for the b/c name conflict (both refer to the same object), might be hackable by sniffing the call-stack, the anonymous [8,9,10] argument has *no* name, and thus your function is stuck. by the way what is **kwargs I can't find any documentation on this? There's a quick example here: http://mail.python.org/pipermail/python-list/2007-June/443934.html -tkc -- http://mail.python.org/mailman/listinfo/python-list
Re: Use list name as string
On Thu, 05 Feb 2009 01:36:17 -, Vincent Davis vinc...@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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Use list name as string
On Thu, Feb 5, 2009 at 3:57 AM, Vincent Davis vinc...@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, -- http://mail.python.org/mailman/listinfo/python-list
Re: Use list name as string
The problem is you seem to be thinking in terms of objects having names. They don't. Names have objects.I agree this is my problem. This is not correct terminology then? The name of the object is anobject Let me give another example and let me know if I am just beating a dead horse. In my current script I have may print statements to verify I am manipulating my list as I hope. I have many lines like; print 'alist', alist I need to print the names so I know what I am looking at on the output. It would be nice if I could define fuction that did this def lp(thelist): magic occurs Then I could just type lp(alist) note I am entering the name which has an object that is a list. I am not entering [1,3,5,7] and it would print 'alist' alist Does it not seem reasonable to what to do this. Vincent Davis 720-301-3003 On Wed, Feb 4, 2009 at 7:55 PM, Rhodri James rho...@wildebst.demon.co.ukwrote: On Thu, 05 Feb 2009 01:36:17 -, Vincent Davis vinc...@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 -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Use list name as string
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 jervi...@gmail.com wrote: On Thu, Feb 5, 2009 at 3:57 AM, Vincent Davis vinc...@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, -- http://mail.python.org/mailman/listinfo/python-list
Re: Use list name as string
On Thu, Feb 5, 2009 at 2:37 PM, Vincent Davis vinc...@vincentdavis.net wrote: 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 Here is a short example, more information on 'Inheritance' can be obtained from http://docs.python.org/tutorial/classes.html class NamedList(list): A List with a 'name' attribute. def __init__(self, name, *args, **keyword_arguments): list.__init__(self, *args, **keyword_arguments) self.name = name you can now do this x = NamedList('x', [1,2,3,4]) x.name 'x' In this example I have inherited all the features of the original built-in list, and added my own initialisation method. of course this is only a minimal example and there are other approaches (such as adding an attribute at runtime) but this one is self documenting to some extent. Cheers, -- http://mail.python.org/mailman/listinfo/python-list
Re: Use list name as string
In article mailman.8810.1233792569.3487.python-l...@python.org, Rhodri James rho...@wildebst.demon.co.uk wrote: Fundamentally, the concept of a single unique name for any object isn't something built into the language (or, indeed, most languages I can think of). An object can have no names (though it'll promptly get garbage collected if it isn't assigned to a name somehow), or just as easily one or many names. Slight tangent: I prefer to use binding target (target for short) for the generic term describing the left-hand side of an assignment statement. Consider L[1] = C() (where L is a list and C is a class) It seems to me that while L and C are properly described as names, L[1] is not really a name, it's an expression describing the location to be used for binding. (The Python docs also use this terminology, at least some parts of them.) -- Aahz (a...@pythoncraft.com) * http://www.pythoncraft.com/ Weinberg's Second Law: If builders built buildings the way programmers wrote programs, then the first woodpecker that came along would destroy civilization. -- http://mail.python.org/mailman/listinfo/python-list
Re: Use list name as string
MRAB goo...@mrett.plus.com wrote: The actual names of the variables and functions shouldn't matter to the outside world; the name of an output file shouldn't depend on the name of a variable. That is a matter of opinion. It is however, an interesting problem, namely: How does one get hold of the actual name by which some parameter is passed? you may want to print, as a debug thingy: print the name passed in was: , ImpossibleThingYieldingName print and it evaluates to: , ArgumentPassed to help you see from where your function was called, and you can't, as far as I know, do this, because there is no backwards link from the object to all of its various names. And while the OP can solve his problem by passing in the name explicitly, as doStuff(banana,banana) it will do him no good if he gets the list returned from something else: doStuff(WhatMustGoHere?,getlist()) Because in such a situation there is no backwards link to a name. So to the OP : Sorry - you can't get there from here. - Hendrik -- http://mail.python.org/mailman/listinfo/python-list