How to avoid "f.close" (no parens) bug?

Roy Smith roy at panix.com
Wed Feb 11 15:26:56 EST 2004


In article <mailman.1479.1076519730.12720.python-list at python.org>,
 Skip Montanaro <skip at pobox.com> wrote:

>     Facundo> What I'm trying to say is: If you're not executing any code (no
>     Facundo> function call) and you're not doing any assingment, what's the
>     Facundo> point of that line?
> 
> There is none, but the Python compiler doesn't know that when it compiles
> the code, so it's not reasonable for it to issue a warning.

The following perfectly reasonable program would change its behavior if 
functions were automatically called when mentioned without trailing ()'s.

------------------------------------

#!/usr/bin/env python

class ReligeousFanatic:
        def surprise ():
                print "Gasp!"

        def fear ():
                print "Run for your lives!"

        def anAlmostFanaticalDevotionToThePope ():
                print "All Hail Discordia!"

spanishInquisition = ReligeousFanatic ()
weaponCount = 0

try:
        spanishInquisition.surprise
        weaponCount += 1
except AttributeError:
        pass

try:
        spanishInquisition.fear
        weaponCount += 1
except AttributeError:
        pass

try:
        spanishInquisition.anAlmostFanaticalDevotionToThePope
        weaponCount += 1
except AttributeError:
        pass

print "We have %d weapons" % weaponCount



More information about the Python-list mailing list