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

M

Michael Chermside

Facundo said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top