How to get the 'name' of an int

Justin Shaw wyojustin at hotmail.com
Fri Feb 28 20:27:06 EST 2003


"Hans Brand" <Hans.Brand at BrandInnovators.com> wrote in message
news:b3najo$hej$1 at reader08.wxs.nl...
> Hi,
>
> I would like to get the 'name' of a variable to do something like this:
>
> causea = 12
> causeb = 13
> for cause in (causea, causeb):
>     print "%s = %s" %(name(cause), cause)
>
> The result should be:
> causea = 12
> causeb = 13
>
> How can I achieve this?
>
> Thanks, Hans
>
>

This doesn't address your problem exactly but is similar.

------------ dpgprint.py -----------------
import inspect
import linecache as lc
import sre
regx = sre.compile('dbgprint\((\w*)\)')

def dbgprint(var):
    lc.clearcache()
    f = inspect.currentframe(1)
    file = inspect.getfile(f)
    lnum = f.f_lineno
    line = lc.getline(file, lnum)
    match = regx.search(line)
    if match:
        print '%s = %s' % (match.group(1), var)
a = 5
dbgprint(a)
----------------------------------------------

The result on import is:
>>> import dbgprint
a = 5
>>>

A very limited solution but handy for simple debug prints.

Justin






More information about the Python-list mailing list