osdir.com


[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
Chris Angelico
>Sent: Friday, August 28, 2015 00:03
>Cc: python-list at python.org
>Subject: Re: How to reassign the value of the variable on runtime?
>
>On Fri, Aug 28, 2015 at 4:25 AM, Ivan Evstegneev <webmailgroups at gmail.com>
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?
>>
> >****************alchemy_data_export.py********************************
> >******
>>
> >engine_object, meta_object = db_existence_check_and_create()
>>
> >def db_new_copy():
>>         engine_object, meta_object = create_new_db()
>>
> >def current_data_export(engine=engine_object, meta=meta_object):
>>     ... code goes here ...
>
>First off, there's one easy - almost trivial - change to make: inside
db_new_copy, declare "global engine_object, meta_object" and then it's able
to change the global (module-level) name bindings.
>
>The second part is a little more tricky: how do you specify a default
argument of "whatever is in the global at the time of the call"? The answer
is: You can't. The normal way to do this is with a sentinel; since it
wouldn't make >sense to use None as your engine, you can write it like this:
>
>def current_data_export(engine=None, meta=None):
>    if engine is None: engine = engine_object
>   if meta is None: meta = meta_object
>
>You lose some of the clarity, but you gain a "lazy link" between the
function default args and the global.
>
>Does that help at all?
>
>ChrisA
>--
>https://mail.python.org/mailman/listinfo/python-list
















Hello Chris,

Sorry for the late response. 

Meanwhile I've played with my code. But I still have some little questions,
if don't mind of course )))))
 
So, the code has been rearranged to this:

**********************alchemy_data_export.py***************************

some "import statements

engine_object = None
meta_object = None


engine_object, meta_object = db_existence_check_and_create()

# In case my db already exists this function just establishes a connections
to it.
# If not, it creates fresh database from file and then connects to it.

def  close_current_connection():
	global engine_object
	global meta_object
	
	engine_object.dispose() #some of the sqlalchemy methods
	engine_object = None
	meta_object = None


def  my_query(engine=engine_object, meta=meta_object)
	.
	.
	some code here for making some default query.
	.
	.

************************************************************************

Now the questions section ^_^.

Assuming I did a following stuff:

>>> from my_project.db_control import alchemy_data_export as alchemy

# In case my db has already been on the server, I would get this msg:
#"Database already exists!"
# Now, suppose I need to close connections

>>>alchemy.close_current_connection()

# Checking my globals

>>>alchemy.engine_object
# Empty line
>>>alchemy.meta_object
# Empty line

# Furthermore I can check that a connection has been closed directly on
MySQL Workbench

# BUT, the party starts here:

>>>alchemy.my_query()

## And it actually establishes a connection to db and returns query results.
## At this moment I was like ---> "o_0  is it still a live? but how?"

Then I decided to rewrite my_query() function so it would look like this:

def my_query():
	engine_object.use_some_method
	meta_object .use_some_method
	.
	.
	the rest of the code
	.
	.

In this situations all works great.  In case I try to use this function
after connection was closed it raises an exception about engine_object and
meta_object.

It looks like, when the module is loaded (imported) for the first time, all
the functions that are defined within it and using a  global varialbles as
their defaults, they would keep the first value of this globals. 

In my case, these are the connection objects produced by
db_existence_check_and_create() function, when the module
"alchemy_data_export" has been loaded for the first time.

Then no matter how hard I try the values of these globals won't be changed
(inside the function defaults). Like they are sticked to the defaults?

So assumed you've already knew about this behavior and suggested to use
"lazy links". Am I right?

Still what is the difference between the way proposed:

***************************************************
>def current_data_export(engine=None, meta=None):
>    if engine is None: engine = engine_object
>   if meta is None: meta = meta_object
****************************************************

and the direct use of these globals within the function?
Like so:

******************************************************
def current_data_export():
	engine_object.some_method_used
	meta_object.some_metheod_used
******************************************************

 In both cases the engine and meta objects will be searched in outerscope,
what is the difference then?


And the last question. 

If I need more than one function to be able to modify globals within a given
module, then each one of the functions should look like this:

**************************************my_module.py**************************
*
my_var_1 = 10
my_var_2 = 20

def my_global_modifier_1():
	global my_var_1
	global my_var_2
	
	my_var_1 += 1
	my_var_2 += 2

def my_global_modifier_2():
	global my_var_1
	global my_var_2
	
	my_var_1 += 3
	my_var_2 += 4 
****************************************************************************
****

At least that the constellation I've tried, and that had been worked fine. 


Thanks a lot and sorry for being verbose ))))

Ivan.