addressalign-toparrow-leftarrow-rightbackbellblockcalendarcameraccwcheckchevron-downchevron-leftchevron-rightchevron-small-downchevron-small-leftchevron-small-rightchevron-small-upchevron-upcircle-with-checkcircle-with-crosscircle-with-pluscontroller-playcredit-cardcrossdots-three-verticaleditemptyheartexporteye-with-lineeyefacebookfolderfullheartglobe--smallglobegmailgooglegroupshelp-with-circleimageimagesinstagramFill 1languagelaunch-new-window--smalllight-bulblinklocation-pinlockm-swarmSearchmailmessagesminusmobilemoremuplabelShape 3 + Rectangle 1ShapeoutlookpersonJoin Group on CardStartprice-ribbonprintShapeShapeShapeShapeImported LayersImported LayersImported Layersshieldstartickettrashtriangle-downtriangle-uptwitteruserwarningyahoo

Re: [PhillyPUG] Python hackery, any better ideas?

From: Mat S.
Sent on: Thursday, January 12, 2012 1:08 PM


On Thu, Jan 12, 2012 at 1:02 PM, Thomas E Jenkins <[address removed]> wrote:
Something based on the above might be more what you are looking for, but just to give another option.

You can leave the hairy function in a module with it referencing all of the module-level (global) vars and just import that module as a variable and manipulate those module level vars to override them.

So if you have a hairy.py with:

frob = 42
other = 23

def somefunc():
    return frob + other

You can then do the below:

>>> hairy_mod = __import__('hairy')
>>> hairy_mod.somefunc()
65
>>> hairy_mod.frob = 88
>>> hairy_mod.somefunc()
111

mind.explode()

This might actually allow me to use the original code without changing it at all. Thanks Thomas!
-Mat 

People in this
Meetup are also in:

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy