align-toparrow-leftarrow-rightbackbellblockcalendarcamerachatcheckchevron-downchevron-leftchevron-rightchevron-small-downchevron-small-leftchevron-small-rightchevron-small-upchevron-upcircle-with-crosscrosseditemptyheartfacebookfullheartglobegoogleimagesinstagramlocation-pinmagnifying-glassmailmoremuplabelShape 3 + Rectangle 1outlookpersonplusImported LayersImported LayersImported Layersshieldstartwitteryahoo

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 

Our Sponsors

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