Search code examples
pythondebuggingvariableserror-reporting

python - undefined variable?


Does python have some undefined variable / debug mode which will output a notice/warning?

PHP allows you to modify the error_reporting to turn on notice warnings which mean doing

<?php
echo $foo;

will throw an "Undefined variable foo on line 2.......

does python have something similar?

I had a bug where I was doing

db.connect

instead of

db.connect()

and I was hoping python would throw a undefined variable connect...

can you bump up the error reporting level or similar in python?


Solution

  • Python complains about undefined variables, without any adjustments to its warning system:

    h[1] >>> print x
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    NameError: name 'x' is not defined
    

    In your case db.connect actually has a value, namely a function object. So your connection isn't undefined.

    h[1] >>> def hello(): pass
         ... 
    h[1] >>> x = hello
    h[1] >>> print x
    <function hello at 0x100461c80>