473,386 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Chapter 9 Tutorial for Classes Not Working

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
4 1217
> 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
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
> 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jeff Louie | last post by:
If you are interested I have added a new chapter on dynamic loading and reflection to my twisted tutorial on OOP in C#. http://www.geocities.com/jeff_louie/OOP/oop13.htm In this chapter I am...
24
by: Alf P. Steinbach | last post by:
The eighth chapter (chapter 2.1) of my attempted Correct C++ tutorial is now available, although for now only in Word format -- comments welcome! Use the free & system-independent Open Office...
5
by: Jeff Louie | last post by:
I have added a chapter on dynamically loading classes (plug ins) using Reflection. Code comments welcome from Jon Skeet or anyone else interested in this. ...
2
by: Jeff Louie | last post by:
If you are interested I have added a new chapter on dynamic loading and reflection to my twisted tutorial on OOP in C#. http://www.geocities.com/jeff_louie/OOP/oop13.htm In this chapter I am...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.