Import a file to namespace

Pedro Machado Santa pedro.santa at gmail.com
Thu Mar 20 17:48:18 CET 2008


Hi all,

I'm really a newbie in Python, and I wanted to do a tricky thing. I
don't know if it is possible but my intention was: I have a package
(namely breve) and I want to alter (override?) some of it's functions
preserving the original library/package - in order if I update it, I
do not lose my "hacks".

Until now I was overriding the functions directcly on my script and
adding them to the package namespace, like this:

import testpackage

class testClass():
#...

testpackage.testClass =  testClass


But this has a problem. If I wanna use that code over many files, I
have to update it manually on all of them every time I update my
"hack". I was thinking if it is possible to keep that code on a file -
namely testpackageaddon.py and import it on my work file so that by
doing that it will automatically override the classes. Like so:

import testpackage
import testpackageaddon

testpackage.testClass() #my hacked class defined on
testpackageaddon.py


And on testpackageaddon.py:

import testpackage

class testClass():
#...

testpackage.testClass =  testClass


Any tips, ideas on how to do this?

Many thanks.

Pedro Machado Santa



More information about the Python-list mailing list