Metaclass Question

Carlos Ribeiro carribeiro at gmail.com
Thu Oct 7 01:23:52 CEST 2004


On Wed, 6 Oct 2004 18:46:47 -0400, Jay Parlar <jparlar at cogeco.ca> wrote:
> My goal here isn't really the whole current_function thing, it's just
> to learn metaclasses.

Well, it's nice on your part admiting it.. because f you only want to
check the current_function, it would be *much* easier (and more
general) to use the inspect module, get the current frame and check
the name of the current code block:

inspect.currentframe().f_code.co_name

> Is there any way to make new instances of a function, short of a lambda?

The answer is *simple*. In fact, it's embarassingly simple. Do it
inside a loop. Really.

for i in range(10):
    def newfunc(...):
        ...
 
... will create as many copies of newfunc as you want; it will bind
all to the same name in the local namespace, but each and every
instance will be different. The func_name attribute will be 'newfunc'
for all functions created, though; but if you're using 2.4, you can
rename them later, by changing the func_name attribute, that is now
writable.

-- 
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