metaclass and customization with parameters

Carlos Ribeiro carribeiro at gmail.com
Tue Oct 5 18:26:04 EDT 2004


On Tue, 5 Oct 2004 18:57:49 -0300, Gustavo Niemeyer
<niemeyer at conectiva.com> wrote:
> > 2) the signature of "def newmethod(self,i=i)" shows another
> > interesting side effect. The value if <i> (defined in the for loop) is
> > passed as a keyword paramenter, and is evaluated whenever the 'def'
> > statement is found. Because it changes, the def is re-run at every
> > loop. In the lack of this hack, the def would run only at the first
> > time, and all methods would in fact be the same.
> 
> That's not true. The def statement is rerun every loop, no matter
> what parameters are used. One can check that by issuing:
> 
> print MyClass.method0.im_func
> print MyClass.method1.im_func

:-P You *right*.  For some reason I was under the impression that the
code was not re-executed everytime -- a kind of optimization, indeed,
if the code does not have any external dependency. But you're right.

Anyway, this is a horrible hack :-) I just wrote it to point out that
classes are much more dynamic in their workings than most people
realize, *specially* if you come from a C++/Java/Delphi background.

-- 
Carlos Ribeiro
Consultoria em Projetos
blog: http://rascunhosrotos.blogspot.com
blog: http://pythonnotes.blogspot.com
mail: carribeiro at gmail.com
mail: carribeiro at yahoo.com



More information about the Python-list mailing list