[Tutor] Basic question -> How to use a class from a file?

Rich Krauter rmkrauter at yahoo.com
Mon Jan 3 04:45:23 CET 2005


Bernard Lebel wrote:
> Okay here comes the next question.
> 
> In that class, I have 3 functions. Actually, one of them calls the two 
> others.
> 
> However when I call these functions (wich are placed before the caller 
> in the file), I get an error saying that the global name x (the function 
> name) is not defined.
> 
> What am I doing wrong?
> 
> 


Hi Bernard,

This is just a guess since you didn't post your code, but did you call 
the 'bare' methods in your class definition, rather than calling the 
methods through an instance (which is usually called 'self' inside class 
definitions)?

I think the following may be similar to what you wanted to do:

class A(object):
     def __init__(self,data=None):
         self.data = data

     def methodA(self):
         self.methodB()
         self.methodC()

     def methodB(self):
         print "in methodB"
	print self.data

     def methodC(self):
	print "in methodC"
	print self.data

if __name__ == '__main__':
     a1 = A(5)
     a1.methodA()

     a2 = A([1,2,3])
     a2.methodA()

Hope this helps.

Rich



More information about the Tutor mailing list