virtual inner classes in python?

Bruno Desthuilliers bdesth.nospam at removeme.free.fr
Tue Sep 9 19:11:09 EDT 2003


kasper graversen wrote:
> hello there.
> 
> I've just started learning python. I see methods are declared virtual by 
> default as in Java. 
No. Methods are virtual. period. No 'declared' and no 'default'.

> Nice. However, the inner class construct seems to be 
> even weaker as that of Java. Not nice! :-( Why are inner classes not 
> virtual?
They are.

> will they be in a near future?
They already are.

> What other language can you 
> recomend, if python cannot provide what I need?

<troll>
Assembly ?-)
</troll>

> My problem is that in the __init__ below, I cannot instantiate "Foo" but 
> have to explicate "Test.foo".. 

Foo is not defined in the global namespace, so you must tell in which 
namespace it is to be found.

> secondly, I want to instantiate the "Foo" 
> in test2 rather than in tester in the current example..

Replace 'lala = Test.Foo()' with 'lala = self.Foo()'.

How can you hope Python to instanciate Test2.Foo when you tell it very 
explicitely to instanciate Test.Foo ?-)

> class Test:
>    def __init__(self):
>        lala = Test.Foo()
>        lala.show()

(I assume that indention went wrong when copying, and that the following 
  line should be at the same level that the __init__().)

>        class Foo:
>        def show(self):
>            print "Test.foo.show"
> 
> 
> class Test2(Test):
>    class Foo:
>        def show(self):
>            print "Test2.foo.show"
> 
> 
> if __name__ == "__main__":
>    start = Test2()
> 
> 

Try this instead :

class Test:
     def __init__(self):
         f = self.Foo()
         f.show()

     class Foo:
         def show(self):
             print 'Test.Foo'

class Test2(Test):
     class Foo:
         def show(self):
             print 'Test2.Foo'


t = Test()
t = Test2()


HTH
Bruno





More information about the Python-list mailing list