Q: Name of a arbitary variable as a string?

Inyeol Lee inyeol.lee at siimage.com
Tue Nov 12 17:12:04 EST 2002


This pseudocode partly works. I use similar function which prints
argument name/value pair during debugging.

def var_name(*args):

    # 1. get caller's stack frame with 'sys._getframe(1)'.
    # 2. get current source line with 'frame.f_code.co_filename, frame.lineno
    #    or equivalent.
    # 3. parse source line and get args tuple of 'var_name' call.

This approach has a lot of limitations :-(

1. var_name(var_name(...)) doesn't work.
2. var_name(...); var_name(...) doesn't work.
3. if renamed or referenced with different name, it doesn't work.
4. It is tricky to get a current source if it is a multi-line statement.
   (anybody who knows how to do this, let me know please.)

On Tue, Nov 12, 2002 at 09:01:28PM +0100, username wrote:
> Hi! 
> 
> Sorry, if this Question was asked a million times!
> 
> I am searching for a function (call it: var_name) that do something like
> this: 
> 
> >>> V="Hello World"
> >>> var_name(V)
> 'V'




More information about the Python-list mailing list