newbie class question

Daniel T. danielt3 at gte.net
Sun Aug 13 21:54:23 EDT 2000


My guess is you want to do something more like:

>>> class MyClass:
...   i = 12345
...   def f( self, number ):
...      return 'hello world ' + `number`
... 
>>> x = MyClass()
>>> print x.i
12345
>>> print x.f( 5 )
hello world 5
>>> print MyClass.i
12345
>>> print MyClass.f( x, 5 )
hello world 5

"Ron Johnson, Jr." <ronjohn at gs.verio.net> wrote:

> What am I doing wrong when trying tp access the method f() ?
> 
> >>> class MyClass:
> ...     i = 12345
> ...     def f(x):
> ...             return 'hello world'
> 
> >>> x = MyClass
> >>> print x.i
> 12345
> >>> print x.f(1)
> Traceback (innermost last):
>   File "<stdin>", line 1, in ?
> TypeError: unbound method must be called with class instance 1st
> argument
> >>> print x.f(self)
> Traceback (innermost last):
>   File "<stdin>", line 1, in ?
> NameError:
>
self                                                                                                

> 
> Sincerely,
> Ron



More information about the Python-list mailing list