[Python-Dev] metaclass insanity

Greg Ewing greg@cosc.canterbury.ac.nz
Tue, 05 Nov 2002 14:04:43 +1300 (NZDT)

Alex Martelli <aleaxit@yahoo.com>:

> I could define that "class Inner" in any place at all, but since it's
> only meant to be used in this one spot, why not define it right here?

Putting it inside the function means the class definition
is executed every time the function is called, which is
rather needlessly inefficient.

You could get most of the namespace benefit by putting
it one level further out, i.e.

  class Foo:

    class Iterator:

    def __iter__(self):
      return self.Iterator(self)

The iterator class would then be known from outside
as Foo.Iterator, which is nicely descriptive and makes
it fairly obvious what it's for.

Greg Ewing, Computer Science Dept, +--------------------------------------+
University of Canterbury,	   | A citizen of NewZealandCorp, a	  |
Christchurch, New Zealand	   | wholly-owned subsidiary of USA Inc.  |
greg@cosc.canterbury.ac.nz	   +--------------------------------------+