How to get the 'name' of an int

Chermside, Michael mchermside at ingdirect.com
Fri Feb 28 12:18:45 EST 2003


> I just wondered if I could write something like:
> 
> for item in (a long list of variables):
>     print "%s = %s" %(name(item), item)
> 
> without having to know each name of each variable

Yes, you can... and now that I've seen your underlying
question, I know what the "design flaw" is that others
were suggesting you might have. Or perhaps I'd be
better off calling it a "design difference", because
I'm sure there are reasons why you might want to use
your original approach in OTHER languages, but probably
not in Python.

The solution is very simple... keep your variables in
a list (or other data structure) in the first place
instead of using a long list of variables. Then THIS bit
of code becomes trivially easy:

    for item in myVariables:
        print "%s  = %s" % (name(item), item)

Of course, I'm being a bit facetious here... there are
two possibilities. One possibility is that you have a
bunch of variables named like this:

    x0 = 0
    x1 = 3
    x2 = 4
    x3 = 45
    x4 = 99

In this case, you should do like I said, and just use
a list:

    x = [0 , 3, 4, 45, 99]

The second possibility is that you have a bunch of
variables named like this:

    user_name = "Karen"
    user_age = 21
    user_id = UserId(30128, access="Normal")
    user_enabled = True

Now, this sounds like a good candidate for being a class
of its own, but let's suppose that we've decided it's
not really worth creating a class for users. In many
other languages, you'd just use a bunch of variables
like this, but in Python, if you plan to perform several
operations on all of the variables at once, you probably
want a dict (they're first-class objects in Python):

    user = {}
    user['name'] = "Karen"
    user['age'] = 21
    user['id'] = UserId(30128, access="Normal")
    user['enabled'] = True

Now it's easy to pass the entire bunch of variables
together, and to perform operations like printing them
all out. So your snippet of code would look like this:

    for item in user:
        print "%s = %s" % (name(item), item)

Now for the final possibility. I'm guessing it's not
as likely as the others, but PERHAPS you really want
to use separate variables. Maybe they're really not
related at all (but if so, why are you trying to do
something to all of them?). One example, I suppose,
would be that if you were writing a debugger for Python, 
you might want it to print out all the local variables
in a function (and their values), but these are clearly
not related variables that belong in a data structure.
Well, in this case, you'd be in luck: Python offers
a way to access the list of local variables (within a
function), by calling the locals() function. So you 
could do something like this:

    def myFunction():
        # do some work here
        if errorHasOccurred:
            for name, value in locals().iteritems():
                print "%s = %s" % (name, value)

Anyway, I hope this helps. If you tell me more about
exactly what you're doing, I might be able to help
pick out a useful data structure to use.

-- Michael Chermside





More information about the Python-list mailing list