How can a module know the module that imported it?

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Wed Nov 11 18:21:26 EST 2009


On Wed, 11 Nov 2009 21:55:58 +0000, kj wrote:

> With Perl one can set a module-global variable before the module is
> loaded.  This provides a very handy backdoor during testing. E.g.

Any time somebody justifies a features as "a very handy backdoor", a 
billion voices cry out and then are suddenly silenced...

[...]
> This does not work in Python, because setting my.module.TESTING variable
> can happen only after my.module has been imported, but by this point,
> the module's top-level code has already been executed, so setting
> my.module.TESTING would have no effect.

(1) Then change your design so you're not relying on changing the 
behaviour of top-level code. Instead of:

chant_incantation(arg)  # effect module.magic, somehow...
import module
do_something_with( module.magic )

(where magic is created by the top-level code)


do this instead:

import module
do_something_with( module.make_magic(arg) )




(2) If you still want a back-door, encapsulate it in another module:


# Contents of module.py

import _secret_backdoor
if _secret_backdoor.manna > 17:
    magic = 42
else:
    magic = 23


Then in your calling code:

# chant an incantation to change the behaviour of module
import _secret_backdoor
_secret_backdoor.manna = 1003

import module
do_something_with(module.magic)


(3) Abuse __builtins__ by polluting it with your own junk:


# Contents of module.py

try:
    my_secret_name12761
except NameError:
    magic = 23
else:
    magic = 42



Then in your calling code:

# chant an incantation to change the behaviour of module
import __builtins__
__builtins__.my_secret_name12761 = None

import module
do_something_with(module.magic)


But if you do this one, hundreds of Python developers carrying flaming 
torches and pitchforks will track you down and do terrible things to your 
corpse...


*wink*




-- 
Steven



More information about the Python-list mailing list