[Python-Dev] metaclass insanity

Guido van Rossum guido@python.org
Mon, 04 Nov 2002 15:54:48 -0500

> > What happens for nested classes?
> >
> > In
> >
> > class X:
> >     class Y:
> >         pass
> >
> > are X.Y instances picklable without extra fiddling?
> Picklable functions and classes must be defined in the top level of a 
> module. Nested classes, and instances thereof, cannot be pickled at all.

BTW, I consider this a flaw.

In the case of nested classes, a possible solution might be for
X.Y.__name__ to be "X.Y" rather than plain "Y".  Then a simple change
to pickle (or to getattr :-) could allow the correct unpickling.

This won't work for classes defined inside functions though -- those
are never picklable.  But making them module-global would be a simple
enough fix (also more efficient, since the class definition code is
executed on every function call).

Can someone provide a reason why you'd want to use nested classes?
I've never felt this need myself.  What are the motivations?

--Guido van Rossum (home page: http://www.python.org/~guido/)