Gotcha — forgetting parentheses

Goto start of series

In Python, omitting the trailing parentheses from the end of a method call (one that takes no arguments) is not a syntax error. The place where this most frequently bites me is with the “close” method on file objects. Suppose you have an output file called “foo” and you want to close it. The correct way to do this is:

foo.close()

However, if you accidentally omit the trailing parentheses, and code this:

foo.close

Python will not report a syntax error, because this is not an error in Python. In Python, this is a perfectly legitimate statement that returns the method object “close”. (Remember that methods are first-class objects in Python.) If you do this in the Python interpreter, you will get a message like this:

<built-in method close of file object at 0x007E6AE0>

The nastiness about this gotcha is that if you fail to code the trailing parentheses on a “close” method for an output file, the output file will not be closed properly. The file’s output buffer will not be flushed out to disk, and the part of the output stream that was still left in the output buffer will be lost. After your program finishes, part of your output file will be missing, and you won’t know why.

The best way of dealing with this gotcha is just to be aware that it can be a problem, and to be alert. Be careful to code the parenthese on your method calls, and especially careful to code them on calls to the “close” method of file objects.

And if you find yourself with an output file that seems to be inexplicably truncated, your first thought should be to check for missing parentheses in the file.close() statement that closes the file.

Programs like PyChecker and PyLint may be able to detect this kind of error, which is one good reason to use them.

4 thoughts on “Gotcha — forgetting parentheses

  1. Hi,

    Just to tell you that it’s not a gotcha at all. The syntax requires you to call a method with parenthesis.

    How would you pass a function reference to another method (a callback method, for example) without this ‘gotcha’ ?

    You don’t have to be particularly carefull about it. If you want to call the method, you need parenthesis. If you need to use the function object for referencing, you don’t add parenthesis.

    But calling it a gotcha is strange to me.

    PS: beside that, thank you for your website. It’s very interesting.

    Regards,

  2. The reason this is a gotcha is that it can bite newbie programmers or programmers unfamiliar with Python. Obviously it’s an important language feature that functions and methods can be passed around as arguments and stored in collections. However, someone new to Python and the concept of functions as first class citizens might expect the interpreter to catch the error just like how they would expect Python to alert them if they had passed 12 parameters to a function that only takes 2. They also might actually expect the function to be called, since languages like Ada allow subroutines with zero parameters to be called without parentheses.

  3. I’m new to python and this gotcha just got me, I spent ages looking for a bug elsewhere in the code because I didn’t know of this feature of python. So I’m just commenting to call olilo an idiot.

  4. The first time I stumbled across code like this was in a programming exam paper. I had a little fun with it as the expense of the lecturer after I became a tutor for the subject🙂

Comments are closed.