Chapter 9 Tutorial for Classes Not Working

T

Tom Grove

Here is my version:

Python 2.4.3 (#2, May 9 2006, 21:56:54)
[GCC 3.4.4 [FreeBSD] 20050518] on freebsd6

I am trying the classes example from the tutorial because some other
class related stuff I am doing is not working either.

Straight from Chapter 9:

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


From here I run:
x = MyClass
xf = x.f
while True:
print xf()

This gives the following error:

Traceback (most recent call last):
File "<stdin>", line 2, in ?
TypeError: unbound method f() must be called with MyClass instance as
first argument (got nothing instead)

Please help...this is killing me!

-Tom
 
D

Damjan

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


From here I run:
x = MyClass

Did you mean x = MyClass()
xf = x.f
while True:
print xf()

This gives the following error:

Traceback (most recent call last):
File "<stdin>", line 2, in ?
TypeError: unbound method f() must be called with MyClass instance as
first argument (got nothing instead)

Please help...this is killing me!

What you are really calling is MyClass.f without any arguments.
 
A

Ant

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()
 
T

tac-tics

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.
 
J

John Salerno

tac-tics said:
Python has some VERY nasty gotchas concerning parenthesis (or lack
there of).

Is this really a gotcha? I don't think you should blame Python for this
mistake. Even a novice programmer like myself can intuitively understand
that parentheses are needed.

And while this *is* something unique to Python (or maybe unique to
dynamic languages in general?), the actual usage of parentheses is
consistent with other programming languages, so Python or not, it should
just make sense to write x = MyClass() instead of x = MyClass
 

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,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top