473,396 Members | 1,765 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Printing variable names

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
Jul 18 '05 #1
8 1422
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

Jul 18 '05 #2
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

Jul 18 '05 #3
Mark McEahern <ma**@mceahern.com> wrote in message news:<ma**************************************@pyt hon.org>...
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.
Jul 18 '05 #4
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
Sc***********@Acm.Org
Jul 18 '05 #5
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 <ma**@mceahern.com> wrote in message news:<ma**************************************@pyt hon.org>...
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

Jul 18 '05 #6
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
Jul 18 '05 #7
[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
Jul 18 '05 #8
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

Jul 18 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Thomas Philips | last post by:
I want to print "1 spam 4 you" using a formatted string that gets its inputs from the dictionary d={'n1':1, 's1':'spam', 'n2':4}. To do so, I write >>> x="%('n1')d %('s1')s %('n2')d you" >>> x...
6
by: Bill | last post by:
Hi I am trying to get my listbox items to print if they stream past the one page mark. my code is working for one page of information (if the e.hasmorepages) is not there. But I am having...
1
by: hamil | last post by:
I am trying to print a graphic file (tif) and also use the PrintPreview control, the PageSetup control, and the Print dialog control. The code attached is a concatination of two examples taken out...
6
by: Siv | last post by:
Hi, I am getting into printing with VB.NET 2005 and want to implement the usual capability that a user can select a selection of pages. I have a report that is generated by my application that if...
0
by: WB | last post by:
Hi, I'm building a C# Windows application that create marketing mailers for sending to customers. This application needs to create a PDF template, a list of customer names and addresses and...
4
by: Lucas Ponzo | last post by:
Hi All, I have an ASP.NET 2.0 app. The users access the pages, uniquely via pocket pc ... I need to print a page. But I need that the page print on a printer installed on the web server...
9
by: s99999999s2003 | last post by:
hi say i have variables like these var1 = "blah" var2 = "blahblah" var3 = "blahblahblah" var4 = "...." var5 = "...".. bcos all the variable names start with "var", is there a way to
4
by: Chris Seymour | last post by:
Hi All, I am working on a python script for my colleague that will walk a directory and search in the different files for a specific string. These pieces I am able to do. What my colleague wants...
3
by: mrsouthg | last post by:
Hi, I am fairly new to c# .net language and have been using a set of code I was given to learn from. I have found a suitable part which I would like to change, I am however finding it a big hurdle...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.