So is not possible to testing if a variable is defined with this functions
vars(), globals(), locals() ?
No, you just need to add another case for __builtins__
The scopes Python consults when looking up a name are:
1. Local scope - locals()
2. Nested function scope(s) - [I don't think these vars can be listed
at runtime]
3. Global scope - globals()
4. Built-ins - __builtins__
If you want to just check whether a variable is currently
defined+accessible, a try-except is much simpler:
var_name = "foo"
try:
eval(var_name)
except NameError:
defined = False
else:
defined = True
However, wanting to test whether a variable is defined or not is
usually a sign of bad code.
Could you explain exactly why you want/need to do such testing?
Cheers,
Chris