Small changes in side library

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sun Sep 23 17:16:20 EDT 2007


En Sat, 22 Sep 2007 22:07:27 -0300, maxim.gavrilov at gmail.com  
<maxim.gavrilov at gmail.com> escribi�:

> I have the side library which provides wide set of different
> functions, but I'm going to replace some of them with mine and
> provided such 'modified' library thought my project.
>
> The following way works well for my purpose:
>
> ------- mylib.py -------
> import sidelib
>
> import os, sys, ....
>
> def func():
>     .....
> sidelib.func = func
> ......
> ?!?!?!?!
> --------------------------
>
> But this cause to write mylib.sidelib.func() to function call, is it
> any way to 'map' definitions from sidelib to mylib (possible at point
> marked ?!?!?!?!) such that constructions like mylib.func() will be
> provided and client code don't see difference between changed and
> original library in syntax way?

Your code already works as you like:
import sidelib
sidelib.func()
and you get the modified function.

You don't have to say mylib.sidelib.func - in fact, mylib.sidelib is the  
same module object as sidelib
But you have to ensure that the replacing code (mylib.py) runs *before*  
anyone tries to import something from sidelib.

> One my idea was to do from sidelib import * and then modify globals()
> dictionary, but this isn't good too because mylib imports many other
> modules and they all mapped into it's namespace (like mylib.os,
> mylib.sys).

As you said, a bad idea.

-- 
Gabriel Genellina




More information about the Python-list mailing list