Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Instance of inherited nested class in outer class not allowed?

Reply
Thread Tools

Instance of inherited nested class in outer class not allowed?

 
 
mrstephengross
Guest
Posts: n/a
 
      02-27-2008
I've got an interesting problem with my class hierarchy. I have an
outer class, in which two nested classes are defined:

class Outer:
class Parent:
def __init__ (self):
print "parent!"
class Child(Parent):
def __init__ (self):
Outer.Parent.__init__(self)
foo = Child()

Note that the second nested class (Outer.Child) inherits from the
first nested class (Outer.Parent). When I run the above code, python
reports a name error:

Traceback (most recent call last):
File "./temp.py", line 3, in ?
class Outer:
File "./temp.py", line 13, in Outer
foo = Child()
File "./temp.py", line 11, in __init__
Outer.Parent.__init__(self)
NameError: global name 'Outer' is not defined

Apparently, python doesn't like having an instance of a derived nested
class present in the outer class. Interestingly enough, if I change
the foo variable to an instance of the parent class:

foo = Parent()

everything is hunky-dory. Is there some syntax rule I'm breaking here?

Thanks!
--Steve
 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      02-27-2008
mrstephengross schrieb:
> I've got an interesting problem with my class hierarchy. I have an
> outer class, in which two nested classes are defined:
>
> class Outer:
> class Parent:
> def __init__ (self):
> print "parent!"
> class Child(Parent):
> def __init__ (self):
> Outer.Parent.__init__(self)
> foo = Child()
>
> Note that the second nested class (Outer.Child) inherits from the
> first nested class (Outer.Parent). When I run the above code, python
> reports a name error:
>
> Traceback (most recent call last):
> File "./temp.py", line 3, in ?
> class Outer:
> File "./temp.py", line 13, in Outer
> foo = Child()
> File "./temp.py", line 11, in __init__
> Outer.Parent.__init__(self)
> NameError: global name 'Outer' is not defined
>
> Apparently, python doesn't like having an instance of a derived nested
> class present in the outer class. Interestingly enough, if I change
> the foo variable to an instance of the parent class:
>
> foo = Parent()
>
> everything is hunky-dory. Is there some syntax rule I'm breaking here?


It's simple - you try to refer to Outer whilst Outer itself is being
created. A much simpler version of your problem is this:


class Foo:
foo = Foo()

You have to live with that. Just do

Outer.foo = Outer.Parent()

after your class-statement to achieve the same result.

Diez
 
Reply With Quote
 
 
 
 
mrstephengross
Guest
Posts: n/a
 
      02-27-2008
> class Foo:
> foo = Foo()
>
> You have to live with that. Just do
> Outer.foo = Outer.Parent()
> after your class-statement to achieve the same result.


Hmmm. Well, I see why that works. It's too bad, though. If I want to
keep all executed code safely within a "if __name__ == '__main__'"
block, it ends up a bit ugly. Then again, I guess this is just an
aspect of python I'll have to get used to. Is there a specific reason
it works this way, by chance?

--Steve
 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      02-27-2008
En Wed, 27 Feb 2008 16:52:57 -0200, mrstephengross
<> escribi�:

>> class Foo:
>> foo = Foo()
>>
>> You have to live with that. Just do
>> Outer.foo = Outer.Parent()
>> after your class-statement to achieve the same result.

>
> Hmmm. Well, I see why that works. It's too bad, though. If I want to
> keep all executed code safely within a "if __name__ == '__main__'"
> block, it ends up a bit ugly. Then again, I guess this is just an
> aspect of python I'll have to get used to. Is there a specific reason
> it works this way, by chance?


class statements (and def, and almost everything in Python) are
*executable* statements, not declarations.
When you import a module, it is executed. If it contains a class
statement, it is executed as follows: create an empty namespace (a dict),
execute the class body in it, create a new class object with __dict__ =
that namespace, and finally, bind the class name to the newly created
class object in the module namespace.
Until that last step, you can't refer to the class being created by name.

I don't get your issue with "if __name__==__main__", but I hope that you
now understand a bit better why a late initialization is required. (Of
course someone could come up with a metaclass to perform that late
initialization, but the important thing is to understand that you cannot
create an instance before the class itself exists)

--
Gabriel Genellina

 
Reply With Quote
 
Diez B. Roggisch
Guest
Posts: n/a
 
      02-27-2008
mrstephengross schrieb:
>> class Foo:
>> foo = Foo()
>>
>> You have to live with that. Just do
>> Outer.foo = Outer.Parent()
>> after your class-statement to achieve the same result.

>
> Hmmm. Well, I see why that works. It's too bad, though. If I want to
> keep all executed code safely within a "if __name__ == '__main__'"
> block, it ends up a bit ugly. Then again, I guess this is just an
> aspect of python I'll have to get used to. Is there a specific reason
> it works this way, by chance?


Well, what would you think python should make of this?

class Foo:

f = Foo()

def __init__(self, argument):
pass


It can't possibly allow to instantiate an object of a class unless the
class creation is finished. Of course it could delay the execution of
anything but method definitions. But then the price would be high - loss
of generatlity, and for example this weren't possible as well:

class Bar:
if relative_moon_moisture() > 10:
def foo(self): pass
else:
def bar(self): pass


Diez
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Want - but cannot get - a nested class to inherit from outer class DBak Python 11 03-08-2008 01:38 PM
nested class inheriting from outer class Richard Lee C++ 4 03-06-2008 09:54 AM
'Class.inherited' v. 'inherited' syntax inside Class 7stud -- Ruby 11 11-09-2007 06:45 PM
Nested repeaters -- accessing data from outer repeater jeremystein@gmail.com ASP .Net 1 11-13-2005 09:15 PM
How to access outer class attribute from nested class Heiko Henkelmann Python 2 04-27-2004 07:37 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57