[Tutor] class and methods/functions

Kent Johnson kent37 at tds.net
Thu Oct 6 19:33:42 CEST 2005


Eric Walker wrote:
> I have a class I am defining and then I call a function within that class. 
> Getting error that function call is not defined. Does the function have to be 
> created anywhere within a class or does it have to be defined before the call 
> within the class.

Actual code and the error message (including the traceback) would be helpful here.

Generally functions (usually called methods in this context) are defined within the body of a class and then called from outside the class. Functions have to be defined before they are  called but not before they are referenced. 

Very simple example:

 >>> class B:
 ...   def foo(self):
 ...     print 'foo'
 ...     self.bar()
 ...   def bar(self):
 ...     print 'bar'
 ...
 >>> b=B()
 >>> b.foo()
foo
bar

Kent



More information about the Tutor mailing list