Re: Chapter 9 Tutorial for Classes Not Working
- From: "Ant" <antroy@xxxxxxxxx>
- Date: 30 Jun 2006 08:15:03 -0700
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()
.
- References:
- Chapter 9 Tutorial for Classes Not Working
- From: Tom Grove
- Chapter 9 Tutorial for Classes Not Working
- Prev by Date: Re: Chapter 9 Tutorial for Classes Not Working
- Next by Date: Re: Way for see if dict has a key
- Previous by thread: Re: Chapter 9 Tutorial for Classes Not Working
- Next by thread: Re: Chapter 9 Tutorial for Classes Not Working
- Index(es):
Relevant Pages
|