class of a class problem

Greg Krohn ('rot-13') 'tert at pncen.hf'.decode
Mon May 12 18:44:00 EDT 2003


"CipoFuzo" <cipofuzo at home.com> wrote in message
news:slrnbc071h.kui.cipofuzo at server.panka.com...
> 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()
>
> test = A()
>
> NameError: global name 'B' is not defined
>
>
>
> Why do I get a namerror? Why is python looking for B in
> the global namespace?
>
> Thanks,
> Cipo

When you do 'self.element = B()' Python looks for 'B' in the local
namespace. When it can't find it there, it looks in the global namespace.
Your 'B' definition isn't in either. That's why you get the error. 'B's
definition is in 'A', so you can do this:

  self.element = A.B()

or

  self.element = self.B()

greg






More information about the Python-list mailing list