class of a class problem

Chad Netzer cnetzer at mail.arc.nasa.gov
Mon May 12 18:28:27 EDT 2003


On Mon, 2003-05-12 at 15:16, CipoFuzo wrote:
> Hello,
> 
> I'm trying to define a class inside of a class
> and it doesn't work:
> 
> class A:
> 	class B:
> 		pass               
> 	def __init__(self):
> 		self.element = B()

> Why do I get a namerror? Why is python looking for B in
> the global namespace?

Because B is NOT declared in the global namespace, and you are not
otherwise specifying a namespace for python to look in, so it checks
local, then global (roughly).  You declared B in the namespace of the A
class.

I tried:

from __future__ import nested_scopes

but that doesn't work it seems...

Oh well, change one line and it will work:

		self.element = A.B()


-- 

Chad Netzer
(any opinion expressed is my own and not NASA's or my employer's)






More information about the Python-list mailing list