Chapter 9 Tutorial for Classes Not Working

Ant antroy at gmail.com
Fri Jun 30 11:15:03 EDT 2006


> class MyClass:
>     "A simple example class"
>     i = 12345
>     def f(self):
>         return 'hello world'

Nothing wrong with this.

>  From here I run:
>     x = MyClass

Here's your problem - x is a class, *not* an instance of a class (i.e.
an object). Methods
operate on objects, not classes. Try x = MyClass() instead to get an
instance of the class.

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

Why not try something simpler - it doesn't look like you really know
what you are doing here. The while True is going to print your "hello
world" until the end of time, and there is no value in assigning the
function to a variable at this stage - its adding complexity which you
don't seem to need at the moment...

Try:

x = MyClass()
x.f()




More information about the Python-list mailing list