metaclass and customization with parameters

Alex Martelli aleaxit at yahoo.com
Wed Oct 6 08:22:12 EDT 2004


Gustavo Niemeyer <niemeyer at conectiva.com> wrote:
   ...
> > >>> class MyClass:
> > ...         for i in range(10):
> > ...                 def newmethod(self,i=i):
> > ...                         print "I'm method #%d" % i
> > ...                 newname = "method%d" % i
> > ...                 locals()[newname] = newmethod
   ...
> As an interesting side effect, the class above will have
> 'newname', 'newmethod', and 'i' attributes, besides 'method0-9'.

...which incidentally shows that i is a +local+ variable...

> Of course, in your example, not using the 'i=i' trick would be a
> problem, since the 'i' variable is lost, and inside the method a
> global is looked for, creating an error.

True, the opcode for i is indeed a LOAD_GLOBAL.  Why _that_ should be
the case is anything but an 'of course' to _me_, though, as we just
showed that i is a +local+.  newmethod is not seen as a nested function
of the anonymous function built from the codeobject's that's the
classbody.  I _think_ there's some adhoccery in the Python compiler (not
the easiest part of Python to read and understand, so I apologize for
any potential confusion here) to specifically ensure that -- I guess
it's for backwards compatibility with old Python versions which didn't
have nested scopes, otherwise any barename in a method would refer first
to the classbody (acting as its 'outer function') which could shadow
globals.  A bit confusing for an "of course", nevertheless;-).


Alex



More information about the Python-list mailing list