![]() |
|
|
|
#1 |
|
mylist = [a, b, c]
I want to print out the names of the variables in mylist (not the values of a, b, and c). How do I go about doing this. Thanks. Mike Mike |
|
|
|
|
#2 |
|
Posts: n/a
|
Mike wrote:
>mylist = [a, b, c] > >I want to print out the names of the variables in mylist (not the >values of a, b, and c). How do I go about doing this. Thanks. > >Mike > > There's no simple answer to this. Consider the fact that you can have more than one name bound to any given mutable instance. What if: a = range(10) b = a mylist = [a] what name do you want printed for the first item in mylist--'a' or 'b'? One idea is to use a dictionary instead. Then: for key, value in mydict.iteritems(): print '%(key)s = %(value)s' % locals() I'm curious what problem you're trying to solve. Cheers, // m Mark McEahern |
|
|
|
#3 |
|
Posts: n/a
|
On Sun, 18 Jan 2004 11:22:08 -0800, Mike wrote:
> mylist = [a, b, c] > > I want to print out the names of the variables in mylist (not the > values of a, b, and c). How do I go about doing this. Thanks. The following example shows that this does not really make sense: a = 1; b = 2; c = 3; mylist = [a, b, c] a = 4; b = 5; c = 6; print mylist print a, b, c Result: [1, 2, 3] 4 5 6 and *not*: [4, 5, 6] 4 5 6 You might want to google for 'Python object reference' etc. Moreover, having a look (e.g. in the tutorial) at how Python passes arguments to functions (mutable and immutable objects) might help to get a better understanding of what is going on behind the scenes. HTH / Nuff Nuff Said |
|
|
|
#4 |
|
Posts: n/a
|
Mark McEahern <> wrote in message news:<mailman.479.1074455535.12720.python->...
> Mike wrote: > > >mylist = [a, b, c] > > > >I want to print out the names of the variables in mylist (not the > >values of a, b, and c). How do I go about doing this. Thanks. > > There's no simple answer to this. Consider the fact that you can have > more than one name bound to any given mutable instance. Why did you specify "mutable"? The same applies to immutable instances. > I'm curious what problem you're trying to solve. So am I. The question shows up here all the time, but I've never seen a reason for it. Dan Bishop |
|
|
|
#5 |
|
Posts: n/a
|
Mark McEahern wrote:
> Mike wrote: >> mylist = [a, b, c] >> >> I want to print out the names of the variables in mylist (not the >> values of a, b, and c). How do I go about doing this. Thanks. >> Mike > ... One idea is to use a dictionary instead. Then: > for key, value in mydict.iteritems(): > print '%(key)s = %(value)s' % locals() > I'm curious what problem you're trying to solve. Mark's questions are very much on target. If the purpose is debugging, you might be satisfied with something like: def vnames(value, *dicts): """From a value and some dictionaries and give names for the value""" result = [] for d in dicts: result.extend([key for key, val in d.iteritems() if val is value]) result.append(repr(value)) return result Which you might use like: a,b,c = 1,2,3 d,e,f = 5,4,3 for v in range(10): print v, vnames(v, locals(), globals()) Note: You probably need only locals or globals if you are at the top level of an interpreter such as Idle or the python shell. You might prefer '==' to 'is', but remember that 0.0 == 0 == 0L. -Scott David Daniels Scott David Daniels |
|
|
|
#6 |
|
Posts: n/a
|
Thanks for the info. That does clear up a few things for me.
This is what I'm trying to accomplish: Basically I have a list of pointers to functions (or whaterver it's called in Python). Something like this: commands = [func1, func2, ...funcN] This is in a script that I use to test an embedded system through the comport. I call the script with the command number (func1 etc...), which calls the corresponding function, which sends a command to the embedded system. I'd like to be able to call the script with --help and have it spit out the list of commands (the names func1, func2 etc...). Mike Mark McEahern <> wrote in message news:<mailman.479.1074455535.12720.python->... > Mike wrote: > > >mylist = [a, b, c] > > > >I want to print out the names of the variables in mylist (not the > >values of a, b, and c). How do I go about doing this. Thanks. > > > >Mike > > > > > There's no simple answer to this. Consider the fact that you can have > more than one name bound to any given mutable instance. What if: > > a = range(10) > b = a > > mylist = [a] > > what name do you want printed for the first item in mylist--'a' or 'b'? > > One idea is to use a dictionary instead. Then: > > for key, value in mydict.iteritems(): > print '%(key)s = %(value)s' % locals() > > I'm curious what problem you're trying to solve. > > Cheers, > > // m Mike |
|
|
|
#7 |
|
Posts: n/a
|
[Me]
> There's no simple answer to this. Consider the fact that you can have > more than one name bound to any given mutable instance. [Dan Bishop] > Why did you specify "mutable"? The same applies to immutable > instances. I didn't think through the immutable issue, that's all. So, in short, no good reason. <wink> Cheers, // m Mark McEahern |
|
|
|
#8 |
|
Posts: n/a
|
Mike wrote:
> This is what I'm trying to accomplish: > > Basically I have a list of pointers to functions (or whaterver it's called > in > Python). Something like this: > > commands = [func1, func2, ...funcN] > > This is in a script that I use to test an embedded system through the > comport. I call the script with the command number (func1 etc...), which > calls the corresponding function, which sends a command to the embedded > system. > > I'd like to be able to call the script with --help and have it spit out > the list of commands (the names func1, func2 etc...). You're lucky, functions "know" their name: >>> def func1(): pass .... >>> def func2(): pass .... >>> for f in [func1, func2]: .... print f.__name__ .... func1 func2 Peter Peter Otten |
|
|
|
#9 |
|
Posts: n/a
|
That makes life easy. Problem solved! Thanks all for the help.
Mike > > You're lucky, functions "know" their name: > > >>> def func1(): pass > ... > >>> def func2(): pass > ... > >>> for f in [func1, func2]: > ... print f.__name__ > ... > func1 > func2 > > Peter Mike |
|
![]() |
| Thread Tools | Search this Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Passing value with out using variable in query string in PHP! | Ali_ggl | General Help Related Topics | 0 | 11-29-2008 12:22 PM |
| Control Printing and Require Authentication for printing with using Java | transformer | Software | 0 | 07-02-2007 04:33 AM |
| Variable Scope in asp.Net | jansi_rk | Software | 1 | 09-18-2006 06:05 PM |
| What's available in variable speed VCR's ? | E. Matthews | DVD Video | 3 | 10-20-2005 11:44 AM |
| I LOVE FULLSCREEN | Lookingglass | DVD Video | 139 | 01-06-2004 02:13 AM |