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

Michael Chermside mcherm at mcherm.com
Fri Feb 13 11:51:27 EST 2004


Facundo writes:
> class Test:
>     def __getattr__(self, name):
>         print "Nobody expects the %s!" % name
> 
> t = Test()
> t.spanish_inquisition = 5
> newvar = t.spanish_inquisition
> 
> But, what's the point to have a...
> 
> t.spanish_inquisition
    [...]
> What I'm trying to say is: If you're not executing any code (no function
> call) and you're not doing any assingment, what's the point of that line?

The point is consistancy. Python the language should be designed to do
what you tell it to, and to have a MINIMUM of special cases and exceptions.
It's a general rule that an expression is a valid kind of statement. Here
is a SENSIBLE use of that:

    myObject.getSize() # this causes myObject to initialize itself

Here are some silly uses:

    23
    t.spanish_inquisition

And here is a VERY IMPORTANT use:

    myList.sort()

Remember... that sort() method RETURNS A VALUE... the value None. Which we
proceed to ignore.

Now, I certainly agree that you don't NORMALLY want to write things like
"t.spanish_inquisition". And that's why pychecker warns about them. But
python (the interpreter) and pychecker serve different purposes. Python
(the interpreter) should just do whatever you tell it to... because if
it won't, then you have no recourse. But pychecker should (and does) warn
you if it thinks what you are doing might be a problem. If you know that
you really DID mean it, then just disregard the warning.

Moral: use pychecker.

-- Michael Chermside




More information about the Python-list mailing list