Calling Function Without Parentheses!
John Machin
sjmachin at lexicon.net
Sun Jan 2 21:43:25 EST 2005
Dan Bishop wrote:
> Kamilche wrote:
> > What a debug nightmare! I just spent HOURS running my script
through
> > the debugger, sprinkling in log statements, and the like, tracking
> down
> > my problem.
> >
> > I called a function without the ending parentheses. I sure do WISH
> > Python would trap it when I try to do the following:
> > MyFunc
> >
> > instead of:
> >
> > MyFunc()
>
> You're a former Pascal programmer, aren't you? ;-)
>
> In Python, it's not an error, because you can do things like:
>
> >>> def simpson(f, a, b):
> ... "Simpson's Rule approximation of the integral of f on [a, b]."
> ... return (b - a) * (f(a) + 4 * f((a + b) / 2.0) + f(b)) / 6.0
> ...
> >>> simpson(math.sin, 0.0, math.pi) # Note that math.sin is a
function
> 2.0943951023931953
In Python, it's not an error, because functions are first class
citizens. The OP's problem is evaluating an expression and then doing
SFA with the result. Pychecker appears to be able to make the
distinction; see below:
C:\junk>type simpson.py
import math
def simpson(f, a, b):
return (b - a) * (f(a) + 4 * f((a + b) / 2.0) + f(b)) / 6.0
print simpson(math.sin, 0.0, math.pi)
C:\junk>python simpson.py
2.09439510239
C:\junk>pychecker simpson.py
C:\junk>c:\python24\python.exe
c:\python22\Lib\site-packages\pychecker\checker.py simpson.py
Processing simpson...
2.09439510239
Warnings...
None
More information about the Python-list
mailing list