[Edu-sig] Factory functions: synthesizing top-level names

kirby urner kirby.urner at gmail.com
Thu May 27 07:52:31 CEST 2010


>
> Much appreciated André!
>
> I think our solutions are similar:
>

... yours is better though, in that you don't make use of exec.

My use of it was superfluous.

Changing my solution, in light of yours:

#===

def makeroot(N):
   try:
       assert type(N)==type(1) and N>=0
   except:
       raise ValueError("0 <= N <= integer")

   fname = "root" + str(N)

   if N==0:
       globals()[fname] = lambda x: pow(x, 0)
   else:
       globals()[fname] = lambda x: pow(x, float(1)/N)


for i in range(11):
    makeroot(i)

#===

Kirby


More information about the Edu-sig mailing list