Inheritance in nested classes

Diez B. Roggisch deets at nospam.web.de
Tue Nov 15 17:40:58 CET 2005


Martin Skou wrote:
> I'm experimenting with using Python for a small web interface, using
> Mark Hammond's nice win32 extensions.
> 
> I use a small class hierarchy which uses inheritance and nested classes.
> 
> Here are a small extract of the code:
> 
> class page:
> 	
> 	def __init__(self):
> 		self.head=[]
> 	
> 	def __str__(self):
> 		...
> 
> 	class simple_tag:
> 		...	
> 	
> 	class p(simple_tag):
> 		...
> 	
> 	class table:
> 		...
> 
> 		class row:
> 			...
> 				
> 			class data:
> 				...
> 	...
> 
> 
> Will this type of code perform noticable slower than unnested classes?

I'm not aware that there is much runtime penalty here - a quick test 
reveals that:

import time

class Foo:
     def __init__(self, baz):
	self.baz = baz

def unnested():
     o = Foo(10)

def nested():
     class Foo:
	def __init__(self, baz):
	    self.baz = baz



then = time.time()
for i in xrange(100000):
     unnested()
print time.time() - then

then = time.time()
for i in xrange(100000):
     nested()
print time.time() - then


Yields

0.839588165283
0.817638158798

for me.

But I hope you are aware that nested classes aren't quite the same as 
they are in java, are you?

Regards,

Diez



More information about the Python-list mailing list