namespace question

Jordan Greenberg greenbergj at wit.edu
Fri May 18 21:39:54 EDT 2007


T. Crane wrote:
> Hi,
> 
> If I define a class like so:
> 
> class myClass:
>     import numpy
>     a = 1
>     b = 2
>     c = 3
> 
>     def myFun(self):
>         print a,b,c
>         return numpy.sin(a)
> 
> 
> I get the error that the global names a,b,c,numpy are not defined.  Fairly 
> straightforward.  But if I am going to be writing several methods that keep 
> calling the same variables or using the same functions/classes from numpy, 
> for example, do I have to declare and import those things in each method 
> definition?  Is there a better way of doing this?
> 
> thanks,
> trevis 
> 
> 

Put the import at the top level, and you forgot self when accessing
attributes.

import numpy

class myClass:
	a=1
	b=2
	c=3

	def myFun(self):
		print self.a, self.b, self.c
		return numpy.sin(a)





More information about the Python-list mailing list