Need to create module with virtual functions to mirror another module

Martin v. Löwis martin at v.loewis.de
Sat Dec 14 07:18:18 EST 2002


> # module newstyle
> import oldstyle
>
> def myfunc(args):
>   return oldstyle.myfunc(*args)
[...]
> But because this is such a mechanical translation, there ought to be a
way
> to consult the globals() function in oldstyle and create these
functions on
> the fly in newstyle. Finding the functions in oldstyle and creating
the
> wrappers isn't too hard. But what I can't work out is how to make the
new
> functions in newstyle accessible by modules that import newstyle.

Where is the problem? If the functions are in newstyle.__dict__, modules
importing newstyle should find them easily.

I would suspect that a loop like

for name, obj in oldstyle.__dict__.items():
   if obj is not a function: continue
   globals()[name] = lambda args, func = obj: func(*args)

should do (if the test for which things should be wrapped is fixed).

Regards,
Martin




More information about the Python-list mailing list