Calling Function Without Parentheses!

Discussion in 'Python' started by Kamilche, Jan 3, 2005.

  1. Kamilche

    Kamilche Guest

    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()

    aaaaaaaaaaaah.
    Kamilche, Jan 3, 2005
    #1
    1. Advertising

  2. Kamilche> I called a function without the ending parentheses. I sure do
    Kamilche> WISH Python would trap it when I try to do the following:
    Kamilche> MyFunc

    Kamilche> instead of:

    Kamilche> MyFunc()

    Google for pychecker.

    Skip
    Skip Montanaro, Jan 3, 2005
    #2
    1. Advertising

  3. Kamilche

    Dan Bishop Guest

    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
    Dan Bishop, Jan 3, 2005
    #3
  4. Kamilche

    John Machin Guest

    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()
    >
    > aaaaaaaaaaaah.


    Aaaaaaaaaaaah indeed. You must be using an extremely old version of
    pychecker. The version I have in my Python22 directory gave the same
    results as the current one; see below.

    C:\junk>type noparens.py
    [bangs inserted to defeat Google's lstrip()
    !def bar():
    ! foo
    !def foo():
    ! alist = []
    ! alist.sort



    C:\junk>pychecker noparens.py

    C:\junk>c:\python24\python.exe
    c:\python22\Lib\site-packages\pychecker\checker.py noparens.py
    Processing noparens...

    Warnings...

    noparens.py:2: Statement appears to have no effect
    noparens.py:5: Statement appears to have no effect
    John Machin, Jan 3, 2005
    #4
  5. Kamilche

    John Machin Guest

    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
    John Machin, Jan 3, 2005
    #5
  6. Kamilche

    Kamilche Guest

    Yep. Definitely time download PyChecker. Thanks for the tip!
    Kamilche, Jan 3, 2005
    #6
  7. Kamilche

    Terry Reedy Guest

    "Kamilche" <> wrote in message
    news:...
    > 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.


    Did you try PyChecker? (Don't know if

    > I called a function without the ending parentheses.


    Which means you didn't call, and that was your problem ;-) In Python, the
    (...) pair in the appropriate context (where an operator is expected) *is*
    the infix/postfix call operator. It is equivalent to the call or gosub
    prefix in some other languages. The call operator works with any callable,
    not just function objects.

    >I sure do WISH Python would trap it when I try to do the following:


    trap = raise an exception? nooooh.

    > MyFunc
    > instead of:
    > MyFunc()


    In Python, non-keyword names resolve at runtime to the objects they are
    then bound to. This simple, uniform rule is, to me, part of the beauty of
    Python. There are lots of times that one wants to refer to a callable
    without calling it. Indeed, because Python separates referring to an
    object from calling the object, it can and does have a broader notion of
    'callable' than other languages. This includes the option of making
    instances of any user class callable (by including a __call__ method).

    Terry J. Reedy
    Terry Reedy, Jan 3, 2005
    #7
  8. Kamilche

    Max M Guest

    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



    Actually you want use a method as an ordinary variable without calling
    it in many cases. It is often used in a dynamic language.

    A simple example is:

    result = []
    a = result.append
    if something:
    a('some result')
    elif something_else:
    a('another result')
    else:
    a('default result')

    --

    hilsen/regards Max M, Denmark

    http://www.mxm.dk/
    IT's Mad Science
    Max M, Jan 3, 2005
    #8
  9. Kamilche

    Kamilche Guest

    Yeah, but still. If they even had the most basic check, like 'an object
    is being referred to on this line, but you're not doing anything with
    it' would be handy in catching that. When you use an object like that,
    usually you're doing something with it, like assigning it to a variable.
    Kamilche, Jan 7, 2005
    #9
  10. Kamilche

    Jeff Shannon Guest

    Kamilche wrote:

    > Yeah, but still. If they even had the most basic check, like 'an object
    > is being referred to on this line, but you're not doing anything with
    > it' would be handy in catching that. When you use an object like that,
    > usually you're doing something with it, like assigning it to a variable.
    >


    In many cases, however, it's not possible to distinguish this.

    def get_pi():
    import math
    return math.pi

    print my_func(get_pi)

    Now, am I trying to pass the function object get_pi into my_func(), or
    do I want to call get_pi() and pass the return value?

    There are also many times when it's sensible to do nothing with an
    object reference -- i.e. ignoring the return value of a function which
    you're calling for its side-effects.

    It seems to me that it's reasonable for the Python interpreter to
    *not* attempt to guess at whether a questionable usage is an error or
    not. Better to have that done by a developer tool (pychecker) than
    through runtime checks every time the program is used.

    Jeff Shannon
    Technician/Programmer
    Credit International
    Jeff Shannon, Jan 7, 2005
    #10
  11. Kamilche

    Kamilche Guest

    Uh, you're right! I wouldn't want to bog Python down with even more
    checking at run time. I guess I'm asking for syntax checks that are
    typically done only with compiled languages.

    It doesn't stop me from using Python, though! Since I don't have syntax
    checking, I find I have to test supporting routines thoroughly, to make
    sure it errs when it should err and runs when it should run. That's
    something that's always good to do, but it's especially useful in
    Python, which has no syntax checking before runtime.
    Kamilche, Jan 7, 2005
    #11
  12. Kamilche

    Nick Coghlan Guest

    Kamilche wrote:
    > Uh, you're right! I wouldn't want to bog Python down with even more
    > checking at run time. I guess I'm asking for syntax checks that are
    > typically done only with compiled languages.


    In that case, I can suggest having a look at Pychecker, which performs static
    sanity checks on Python code.
    http://pychecker.sourceforge.net/

    Cheers,
    Nick.

    --
    Nick Coghlan | | Brisbane, Australia
    ---------------------------------------------------------------
    http://boredomandlaziness.skystorm.net
    Nick Coghlan, Jan 8, 2005
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    7
    Views:
    9,189
    Jeff Dillon
    May 17, 2006
  2. Jerry He

    functions without parentheses

    Jerry He, Jul 28, 2005, in forum: Python
    Replies:
    15
    Views:
    663
    Dan Sommers
    Jul 31, 2005
  3. Chris McDonald

    Using sizeof without parentheses

    Chris McDonald, Apr 10, 2006, in forum: C Programming
    Replies:
    5
    Views:
    363
    Chris McDonald
    Apr 10, 2006
  4. John Goche
    Replies:
    3
    Views:
    272
    Marcus Kwok
    Sep 14, 2006
  5. Excel User

    Cannot use parentheses when calling a Sub

    Excel User, Jun 26, 2009, in forum: ASP General
    Replies:
    3
    Views:
    304
    Daniel Crichton
    Jun 30, 2009
Loading...

Share This Page