views:

56

answers:

4

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?

+7  A: 

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>
The MYYN
A: 

As far as I know, there is no such option in Python. However, there are tools like PyChecker, which will help you finds bugs which in other languages are caught by a compiler.

Helper Method
+3  A: 

This is not an undefined variable. You refer to the existing method connect - if there wasn't one, you'd get a NameError as The MYYN shows - but you don't call it. That's perfectly valid as far as the language is concerned - in fact, this behaviour (when part of a bigger expression) is sometimes extremely useful. But let alone, it's pointless of course. I suppose static analysis tools such as pylint or PyChecker might complain about this (I'm not sure though, I rarely make this kind of mistake) and using them won't hurt anyway.

delnan
A: 

You can use a tool like pylint http://www.logilab.org/project/pylint

Example

class A(object):
    def fun():
        return None

a = A()
a.fun

Pylint snippet warning:

R:  2:A.fun: Method could be a function
kevpie