Forgetting "()" when calling methods

Alex Martelli aleax at aleax.it
Sun Apr 27 03:38:47 EDT 2003


Andrew Bennetts wrote:
   ...
> For that matter, removing __nonzero__ from functions still doesn't help
> this case:
> 
>     sys.exit
> 
> So it's of pretty minimal benefit.

Not really.  Write the following erro.py file:

import sys
sys.exit

and now run:

[alex at lancelot booksa]$ pychecker erro.py
Processing erro...

Warnings...

erro.py:2: Statement appears to have no effect
[alex at lancelot booksa]$

See?  PyChecker CAN and DOES warn about "this case"!  But OF COURSE
it can't help with the following erro1.py:

import sys
if sys.exit:
    print 'ha!'

[alex at lancelot booksa]$ pychecker erro1.py
Processing erro1...
ha!

Warnings...

None
[alex at lancelot booksa]$

So, _if_ we could avoid functions and methods being usable as bools,
we'd help get (ASAP, at runtime) some errors that PyCheccker *CANNOT*
get through its static analysis.

This may not be doable if we need to support the given getattr idiom
f=getattr(sys,'whatever',None)
if f: ...
rather than requiring an "if f is None:".  And it may not be worth
doing, since the people who I think would be helped by this appear to
insist PyChecker should do it (apparently getting a prompt runtime
error isn't sufficient for them -- it must be PyChecker) and PyCheker
just can't.  But, for those of us that see PyChecker and runtime errors
in SINERGY (i.e., things are reasonably fine as long as AT LEAST ONE
of these catches an error early), the benefit wouldn't be minimal, IMHO.


Alex





More information about the Python-list mailing list