Reputation: 12712
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?
Upvotes: 1
Views: 13236
Reputation: 26108
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
Upvotes: 0
Reputation: 188134
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>
Upvotes: 9
Reputation:
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.
Upvotes: 3
Reputation: 62224
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.
Upvotes: 0