class of a class problem
CipoFuzo
cipofuzo at home.com
Mon May 12 22:10:03 EDT 2003
Thanks for all the replies!
It makes total sense the way it works.
Cipo
In article <b9p83i02s47 at enews3.newsguy.com>, Greg Krohn wrote:
>
> "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