function without brackets ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stef Mientki

    function without brackets ?

    If I call a parameterless function without brackets at the end,
    the function is not performed, but ...
    I don't get an error message ???

    Is this normal behavior ?

    thanks,
    Stef Mientki
  • Sebastian 'lunar' Wiesner

    #2
    Re: function without brackets ?

    Stef Mientki <S.Mientki-nospam@mailbox. kun.nltyped
    If I call a parameterless function without brackets at the end,
    the function is not performed, but ...
    If you omit the brackets, you don't actually call the function. Instead
    you get a reference to the function object.

    Consider this example:

    cwd = os.getcwd()
    # cwd now contains a string, denoting the current working directory
    func = os.getcwd
    # func now contains a reference to the function os.getcwd
    print func == os.getcwd # prints True
    # you can even call it:
    cwd_2 = func()
    # cwd_2 now also contains a string with the current directory.
    print cwd == cwd_2 # prints True, too

    --
    Freedom is always the freedom of dissenters.
    (Rosa Luxemburg)

    Comment

    • rzed

      #3
      Re: function without brackets ?

      Stef Mientki <S.Mientki-nospam@mailbox. kun.nlwrote in news:57668
      $459bbf95$d443b b3a$16218@news. speedlinq.nl:
      If I call a parameterless function without brackets at the end,
      the function is not performed, but ...
      I don't get an error message ???
      >
      Is this normal behavior ?
      >
      Yes, it's normal, but you did not in fact call the function, if you
      were using Python. Functions are objects that, for example, can be
      passed to other functions. The way to refer to the functions
      themselves is to omit the arguments and parentheses. So by simply
      stating the name of a function, that's all you've done.

      --
      rzed

      Comment

      • Carsten Haese

        #4
        Re: function without brackets ?

        On Wed, 2007-01-03 at 15:37 +0100, Stef Mientki wrote:
        If I call a parameterless function without brackets at the end,
        the function is not performed, but ...
        I don't get an error message ???
        >
        Is this normal behavior ?
        Yes. If you "call" a function without brackets, it's not a call.

        Remember that functions are first class objects that can be passed
        around like any other object. Hence, Python needs the distinction
        between

        x = foo() # Assign the *result* of calling foo to x

        and

        x = foo # Assign the *function* foo itself to x.

        You don't get an error message because a function name without
        parentheses is a valid expression that refers to that function.

        Hope this helps,

        Carsten.


        Comment

        • Kay Schluehr

          #5
          Re: function without brackets ?


          Stef Mientki schrieb:
          If I call a parameterless function without brackets at the end,
          the function is not performed, but ...
          I don't get an error message ???
          >
          Is this normal behavior ?
          Yes, this is perfectly o.k. because each function is a first class
          citizen in Python. The difference between foo() and foo is simply that
          foo() is the value returned by the function call on foo and foo is a
          function object.

          Kay

          Comment

          • Stef Mientki

            #6
            Re: function without brackets ?

            Hope this helps,
            >
            thanks You all guys,
            It's perfectly clear to me now !

            cheers,
            Stef

            Comment

            Working...