another easy (im sure) newbie question
gerrit at nl.linux.org
Wed Mar 1 15:28:56 CET 2000
<quote name="Shaun Hogan" date="951916062" email="shogan at iel.ie">
> ok i started learning python a week ago.
> ive followed Guido's tutorial and another one, i have a good grasp of everthing up to Classes, now the example in the tutorial 9.3.2:
> class MyClass:
> "A simple example class"
> i = 12345
> def f(x):
> return 'hello world'
> is where i am at.
> the tutorial says "MyClass.i and MyClass.f are valid attribute references, returning an integer and a function object,"
> MyClass.i returns "12345" fine...but MyClass.f gives me "<unbound method MyClass.f>"
> how do i get it to return "hello world" or whatever its supposed to do?
> also what does the error message "TypeError: unbound method must be called with class instance 1st argument" mean???
> simple answers im sure, but its got me stuck.
You'll need to create an instance of the class first. If you call a
class, an instance gets returned:
<__main__.Myclass instance at 813a9f8>
you can now access those methods (aka class functions) through this instance:
You want to bind it to a name first, because otherwise, you're class
get destroyed immediatly:
>>> m = Myclass()
Those instances are useful if you want something to be different every
time. This can be done with the first argument of a method, in your
I hope you can get a little further through the tutorial now. How much
programming experience do you have? And how much object oriented
programming experience? If it's not much, I recommend buying a book:
Learning Python, for example.
-----BEGIN GEEK CODE BLOCK----- http://www.geekcode.com
GCS dpu s-:-- a14 C++++>$ UL++ P--- L+++ E--- W++ N o? K? w--- !O !M !V PS+ PE?
Y? PGP-- t- 5? X? R- tv- b+(++) DI D+ G++ !e !r !y
-----END GEEK CODE BLOCK-----
More information about the Python-list