1

Is there a way to know where a variable was defined in python (e.g. file name)?

I'm experiencing the following problem: in one of project's source file, there is a "print variable" statement, and variable isn't defined in that file, and I can't find where it is defined variable.

7
  • You'll have to give us a lot more. Not for variables, but for objects you can usually determine where they were defined, yes. Commented Dec 17, 2012 at 14:59
  • 1
    Hate being downvoted without knowing the reason. Commented Dec 17, 2012 at 15:08
  • 3
    I did not downvote your question, but the initial version was way to underspecified. The extra information you added shows you actually want to know something completely different from what two people posting answers had to guess for. Commented Dec 17, 2012 at 15:08
  • 1
    If this is for a one-time task, why not just use a tool like grep (or ack-grep) to search the code for other references to that variable? Commented Dec 17, 2012 at 15:10
  • You'll have to include more information. The print succeeds? Did you grep for the variable name? Is this in a function, a class, a module? Commented Dec 17, 2012 at 15:11

2 Answers 2

2

No, not for variables. Variables are just references to arbitrary python objects, and if you can find the variable, you have found the function, class, or the module it is defined in.

I suspect you wanted to know where python objects are defined in instead; for many objects that can be determined from the .__file__ attribute of the object. For classes, you'd have to traverse back it's module first, for methods, traverse to it's function, for functions, find it's code object, for code objects, use their .co_filename attribute, etc.

Or use the getsourcefile() function to automate that process:

>>> import inspect
>>> inspect.getsourcefile(inspect.getsourcefile)
'/usr/lib/python2.6/inspect.py'
Sign up to request clarification or add additional context in comments.

5 Comments

I was meaning the name, so I think it can't be helped. Thanks anyway.
@gg.kaspersky: What do you mean by name then? You can inspect the code object, see what local variables are attached to it, get line offsets for the bytecode that refer to them, etc. Painful, but possible.
Basically, I see a variable, and I don't understand how python knows what it is, my brain would raise a NameError exception instead :). Clearly, it is defined in some outer scope, but I need to investigate.
@gg.kaspersky: If you have a reference to a function or code object, you can get quite far to figure that out from Python code. But that's not for the faint-of-heart, and requires some in-depth knowledge of Python. Why not just grep for the variable name?
I was wondering if python offers a direct solution. I search the web and didn't find anything, so I asked here.
0

You can use inspect module to get information where object was defined: http://docs.python.org/2/library/inspect.html#inspect.getsourcefile

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.