Chapter 9 Tutorial for Classes Not Working

tac-tics tactics40 at gmail.com
Fri Jun 30 11:33:55 EDT 2006


>     x = MyClass
>     xf = x.f
>     while True:
>        print xf()

Python has some VERY nasty gotchas concerning parenthesis (or lack
there of).

In the first line here, you assign x = MyClass. That means x becomes an
alias for the class MyClass..... not an object like you intended. Look
back at the tutorial. You simply left of the parenthesis.

Another time this may cause you headaches is with functions:

#!/usr/bin/python
def fun():
    print "Hello world!"

if __name__ == "__main__":
    fun

Run this script, and you will get no output at all. Forgetting the ()
on the end of "fun" on the last line makes the difference between
executing the function and returning a reference to it.




More information about the Python-list mailing list