How to reassign the value of the variable on runtime?

Michael Torrie torriem at gmail.com
Thu Aug 27 20:49:36 CEST 2015


On 08/27/2015 12:25 PM, Ivan Evstegneev wrote:
> Can some please (I mean very please) explain me how do I reassign
> "engine_object" and "meta_object" variables, 
> so they would store(point to) a new connection objects of my database,
> while other functions still would see those variables as their defaults? 

If I understand you, the solution is fairly simple. Forgive me for not
referencing your actual code.  But I think you'll get the picture.
Simply don't do:

from foo import *

or even

from foo import bar

When you do the wildcard import, this puts references to all the objects
in foo (or just foo.bar) into your current module's namespace. If you
reassign these names, it just rebinds the name in your current module
space, not in the imported module's namespace.

So the solution is this:

import foo

foo.bar = "something"

Now in every module that imports foo, foo.bar picks up the change
because the rebinding happens inside that module's namespace.  Hope this
makes sense.



More information about the Python-list mailing list