function python variables

How to get the original variable name of variable passed to a function [duplicate]


Is it possible to get the original variable name of a variable passed to a function? E.g.

foobar = "foo"

def func(var):
    print var.origname

So that:





All I was trying to do was make a function like:

def log(soup):
    f = open(varname+'.html', 'w')
    print >>f, soup.prettify()

.. and have the function generate the filename from the name of the variable passed to it.

I suppose if it’s not possible I’ll just have to pass the variable and the variable’s name as a string each time.


  • 2

    No. Perhaps if you describe what you are trying achieve on a higher level, we can give you some pointers or alternative solutions?

    May 1, 2010 at 12:00

  • 1

    I’m mainly wondering why you would want it? Afaik it’s not possible, never heard of anybody wanting to do it before though.

    – dutt

    May 1, 2010 at 12:01

  • Related: Getting the name of a variable as a string

    – wjandrea

    Dec 16, 2019 at 23:02

  • @wjandrea clear duplicate IMO.

    Oct 15 at 6:09


You can’t. It’s evaluated before being passed to the function. All you can do is pass it as a string.


  • Is there then maybe a way to take a variable and save its name as a string?

    – Acorn

    May 1, 2010 at 12:07

  • 3

    You could access the locals() and globals() dictionaries and look for variables that match the value, but that’s really inelegant. Better to simply pass it manually: log('myvar', myvar).

    May 1, 2010 at 12:10

  • 12

    This answer is clearly wrong, and the answer below from @Aeon is correct.

    – vy32

    Aug 6, 2019 at 18:42

  • “Is there then maybe a way to take a variable and save its name as a string?” Think of it the other way around: if you have multiple things that you want to pass to the function, why are they separately named variables in the first place? Instead, collect related pieces of information that need an identifying “key”, into the data structure designed for that exact purpose: a dict. See also:

    Jun 28 at 21:01


To add to Michael Mrozek’s answer, you can extract the exact parameters versus the full code by:

import re
import traceback

def func(var):
    stack = traceback.extract_stack()
    filename, lineno, function_name, code = stack[-2]
    vars_name = re.compile(r'\((.*?)\).*$').search(code).groups()[0]
    print vars_name

foobar = "foo"


# PRINTS: foobar


  • If you want to make this into a function, just have stack[-3] instead of stack[-2].

    – eddys

    Jan 28 at 9:01


Looks like Ivo beat me to inspect, but here’s another implementation:

import inspect

def varName(var):
    lcls = inspect.stack()[2][0].f_locals
    for name in lcls:
        if id(var) == id(lcls[name]):
            return name
    return None

def foo(x=None):
    lcl="not me"
    return varName(x)

def bar():
    return foo(lcl)

# 'lcl'

Of course, it can be fooled:

def baz():
    return foo(lcl)

# 'x'

Moral: don’t do it.


  • This is a really smart solution, but it won’t catch things like f(obj.attr).

    Nov 10, 2021 at 14:33