By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,301 Members | 1,741 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,301 IT Pros & Developers. It's quick & easy.

Chapter 9 Tutorial for Classes Not Working

P: n/a
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
Jun 30 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
> 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.
--
damjan
Jun 30 '06 #2

P: n/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()

Jun 30 '06 #3

P: n/a
> 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.

Jun 30 '06 #4

P: n/a
tac-tics wrote:
x = MyClass
xf = x.f
while True:
print xf()


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
Jun 30 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.