How to know which variables are defined?

Asked

Viewed 134 times

1

Is there a function that returns the name of the variables defined in a Python instance? Something similar to the command who in Matlab that returns the names of the user-defined variables.

2 answers

3

Yeah, I don’t know if I’d like that:

  • dir() list the variables of the specific scope
  • globals() returns global variables (which should be avoided)
  • locals() returns the function variables

3


According to the Stack in English would be as follows:

  • dir() returns a list of variables in the scope
  • globals() returns a dictionary of global variables
  • locals() returns a dictionary of local variables
  • These functions serve, but it is not the way I would like because they also return everything that is set automatically and also by the imports of modules. I made a script to filter by variable type.

  • In the link in English has several different examples that may help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.