[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How to reassign the value of the variable on runtime?

-----Original Message-----
>From: Python-list
[mailto:python-list-bounces+webmailgroups=gmail.com at python.org] On Behalf Of
Michael Torrie
>Sent: Thursday, August 27, 2015 21:50
>To: python-list at python.org
>Subject: Re: How to reassign the value of the variable on runtime?

>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

Hi Michael,

Thanks for the reply, I've got your point, furthermore tried to make imports
as you've told but ended up with kind of an opposite result.

Maybe I've explained myself not clear enough. If you don't mind I'll bring
back the piece of my code here. )))

So the package/module tree looks like that:


The "alchemy_data_export.py" looks like this(more concise version):

relevant import here

engine_object, meta_object = db_existence_check_and_create()

def rebuild():
   	 engine_object, meta_object = db_existence_check_and_create()

def current_data_export(engine=engine_object, meta=meta_object):
	  ...some important code here ^_^
	print("System's Data successfully loaded into Database!")


So I did the stuff your way:

>>> import  my_proj.control.db.alchemy_data_export as alchemy

# after this line I've get the message from
"db_existence_check_and_create()" that all is good 
# and now I can load my data into it.
# The "engine_object" and "meta_object" were initialized
# So I did the following

>>> alchemy.current_data_export() # no arguments here, cause they've just
been initialized
>>> "Data has been loaded!"

# Now lets say, I want to drop my old db and create new one with new data.
# so I've used some function in alchemy


# This is the moment of truth. ))) 
# The next step was to rebuild the db and reinit the variables
"engine_object" and "meta_object"
# Did the following

>>> alchemy.rebuild()

# BUT!! now I can't write something like this:


# Instead:


# I do not want to do this. I'm looking for the (right) way of just reinit
the "engine_object" and "meta_object"
#so they'll be accepted by current_data_export() function as its defaults.
# Another words, just want to keep on using a current_data_export() as I did
it the first time, without the need of passing the arguments.

How can it be done?

Thanks a lot,

P.S. As for the wildcard imports, I use an __all__ = [list of what to
import] on the "other side" so I do limit my imports (hopefully). 

The reason for this, at least from my point of view (just a student with 2
min of experience ^_^), is the desire to limit background functions from
being displayed in auto-complete view of my IDE. 
By this (again IMHO), if someone would use my API in future, he want be
confused of which function to use. 

But, in case, there is more elegant way  to achieve this, will be glad to