Re: Chapter 9 Tutorial for Classes Not Working



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
.



Relevant Pages

  • Re: adding methods at runtime
    ... myattr = "myattr" ... def method: ... instance.method = new.instancemethod(method, instance, myclass) ... The lookup mechanism will then invoke the descriptor protocol on the function object, which will return a method. ...
    (comp.lang.python)
  • Re: Dynamically generating classes?
    ... >> def initialize ... > def create_class(name, parent = nil) ... > klass = Class.new ... "MyClass" ...
    (comp.lang.ruby)
  • Re: overloading *something
    ... >>> How does one make an arbitrary class ) behave ... >>> I've looked at getitem, getslice, and iter. ... >> def totuple: ... >> myargs = myclass() ...
    (comp.lang.python)
  • Re: Class Methods help
    ... we have "instance method". ... class MyClass: ... def some_func: ...
    (comp.lang.python)
  • Re: return same type of object
    ... This is usually known as a 'factory method'. ... return MyClass() ... def create: ... return PsychoRigid() ...
    (comp.lang.python)