virtual inner classes in python?

  • Thread starter kasper graversen
  • Start date
K

kasper graversen

hello there.

I've just started learning python. I see methods are declared virtual by
default as in Java. Nice. However, the inner class construct seems to be
even weaker as that of Java. Not nice! :-( Why are inner classes not
virtual? will they be in a near future? What other language can you
recomend, if python cannot provide what I need?

My problem is that in the __init__ below, I cannot instantiate "Foo" but
have to explicate "Test.foo".. secondly, I want to instantiate the "Foo" in
test2 rather than in tester in the current example..

class Test:
def __init__(self):
lala = Test.Foo()
lala.show()
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()



hope to hear from you soon ;)

\kasper
 
P

Peter Hansen

kasper said:
I've just started learning python. I see methods are declared virtual by
default as in Java. Nice. However, the inner class construct seems to be
even weaker as that of Java. Not nice! :-( Why are inner classes not
virtual? will they be in a near future? What other language can you
recomend, if python cannot provide what I need?

My problem is that in the __init__ below, I cannot instantiate "Foo" but
have to explicate "Test.foo".. secondly, I want to instantiate the "Foo" in
test2 rather than in tester in the current example..

class Test:
def __init__(self):
lala = Test.Foo()
lala.show()
class Foo:
def show(self):
print "Test.foo.show"

With the tabs that you used, the above formatting is what some of us
saw. It seems you meant for "class Foo" to appear at the same level
of indentation as the "def __init__", right?

As for "declared virtual by default", I don't think this is quite
correct for Python. All methods are always virtual in Python,
unless one goes to extremes to simulate some other effect, I think.

Anyway, I believe the problem you are experiencing stems from a lack
of understanding of how Python finds things in its "namespaces".
This is a key concept to issues such as the one you are investigating,
and you won't get far if you assume Python is anything like Java
or other static languages in this respect.

First, you need to specify Test.Foo because otherwise you are
asking for a Foo that is found in the global (module) namespace.
You could also use "self.Foo" in this case, though it might be
considered slightly less clear, but I have a feeling it would
do roughly what you want, whatever that is. (You didn't really
make it clear what you want, as near as I can tell.)

-Peter
 
B

Bruno Desthuilliers

kasper said:
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 ?-)
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
 
J

John Roth

kasper graversen said:
now, since you are confused(?) with my code posting, how should I go about
posting python code the next time I want to do so?

Use spaces, not tabs. A fairly large number of newsreaders do not
handle tabs correctly. The general python recommendation is to always
use spaces for indentation, never tabs and absolutely never mixtures of
spaces and tabs. Most python aware editors can be set up to use spaces
conveniently.

John Roth
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,888
Messages
2,569,964
Members
46,293
Latest member
BonnieHamb

Latest Threads

Top