Printing variable names
Mark McEahern
mark at mceahern.com
Sun Jan 18 14:39:57 EST 2004
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
More information about the Python-list
mailing list