Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #95753

RE: How to reassign the value of the variable on runtime?

Path csiph.com!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail
Return-Path <webmailgroups@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'args': 0.04; 'modify': 0.04; 'none:': 0.05; 'friday,': 0.07; 'great.': 0.07; 'raises': 0.07; 'rewrite': 0.07; 'subject:How': 0.09; 'assumed': 0.09; 'globals': 0.09; 'msg:': 0.09; 'statements': 0.09; 'variables,': 0.09; 'exception': 0.13; 'question.': 0.13; 'def': 0.13; 'argument': 0.15; 'message-----': 0.15; 'server,': 0.15; 'variables': 0.15; '4:25': 0.16; '>does': 0.16; '>on': 0.16; 'alchemy': 0.16; 'all?': 0.16; 'bindings.': 0.16; "can't.": 0.16; 'chris,': 0.16; 'defaults,': 0.16; 'function?': 0.16; 'globals.': 0.16; 'ivan': 0.16; 'link"': 0.16; 'meanwhile': 0.16; 'query.': 0.16; 'skip:> 20': 0.16; 'skip:[ 60': 0.16; 'sqlalchemy': 0.16; 'subject:variable': 0.16; 'then?': 0.16; 'verbose': 0.16; 'wrote:': 0.16; "wouldn't": 0.16; 'exists': 0.18; 'module,': 0.18; 'creates': 0.18; 'variable': 0.18; '>>>': 0.20; '2015': 0.20; 'aug': 0.20; 'suggested': 0.20; 'not,': 0.22; 'assuming': 0.22; 'function,': 0.22; 'skip:* 50': 0.22; 'suppose': 0.22; 'am,': 0.23; 'code.': 0.23; 'defined': 0.23; 'this:': 0.23; 'second': 0.24; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'module': 0.25; "i've": 0.25; 'rest': 0.26; 'chris': 0.26; 'skip:" 20': 0.26; 'skip:m 30': 0.27; 'checking': 0.27; 'fri,': 0.27; 'least': 0.27; 'connects': 0.27; 'skip:e 30': 0.27; 'specify': 0.27; 'function': 0.28; 'values': 0.28; 'looks': 0.29; 'objects': 0.29; 'starts': 0.29; 'connection': 0.30; 'url:mailman': 0.30; 'code': 0.30; 'connections': 0.30; 'query': 0.30; 'skip:. 10': 0.32; 'changed': 0.33; 'url:python': 0.33; 'right?': 0.33; 'message- id:@gmail.com': 0.34; 'url:listinfo': 0.34; 'case,': 0.34; 'file': 0.34; 'worked': 0.34; 'received:google.com': 0.35; 'so,': 0.35; 'skip:c 30': 0.35; 'fresh': 0.35; 'skip:> 10': 0.35; 'but': 0.36; 'should': 0.36; 'url:org': 0.36; 'email addr:python.org': 0.36; 'cases': 0.36; 'loaded': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'thanks': 0.37; 'charset:us-ascii': 0.37; 'difference': 0.38; 'late': 0.38; "won't": 0.38; 'mean': 0.38; 'goes': 0.39; 'subject:the': 0.39; 'skip:e 20': 0.39; 'behavior': 0.61; 'close': 0.61; 'default': 0.61; 'engine': 0.62; 'making': 0.62; 'course': 0.62; 'matter': 0.63; 'lose': 0.63; 'more': 0.63; 'here:': 0.63; 'within': 0.64; 'between': 0.65; 'response.': 0.66; '****': 0.66; 'python-list': 0.66; 'here': 0.66; 'decided': 0.66; 'email name:python-list': 0.67; 'results.': 0.67; 'direct': 0.68; 'skip:* 70': 0.70; 'august': 0.75; 'received:109': 0.75; 'gain': 0.82; '--->': 0.84; '>def': 0.84; '>you': 0.84; 'off,': 0.84; 'please)': 0.84; 'stuff:': 0.84; 'subject:value': 0.84; 'played': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:references:in-reply-to:subject:date:message-id:mime-version :content-type:content-transfer-encoding:thread-index :content-language; bh=9A1t0z7BYHVjW5/+PAOsNN4a9Dx0jGWOHqlgjln5khI=; b=VxMZTJbj1lQG2WJyG8vJfC56IriNwGWRT9uCTm9S4EG4nwqqJBm6HfL4+8fYBESfIB s92OmvJnaMucd3RQg1ox/8V2tAc31gKYpW1NSO2coj6P5z4RZAT7tAZxRfvZSgP1BZ6W o1S7A3eW+ca9izA0xtWNSYC7SIfGRQRCswyDm1EVz/0+0LabA3YsW53V56Yna2rdR44w Uzq229+q2zMo9TYsAN0fMszOeV1tS/TCB1CkIRkn8ISWC4FRYGKgTLi9KZiIsR8XtPbM ZQEwrqto60REr86wlgqA4DyQlF8KeMPooGjp/jdIuw5VDkf0NlNZr72QEiyIFI4rodoH 7HPA==
X-Received by 10.180.109.17 with SMTP id ho17mr6368402wib.34.1440863181742; Sat, 29 Aug 2015 08:46:21 -0700 (PDT)
From "Ivan Evstegneev" <webmailgroups@gmail.com>
To <python-list@python.org>
References <002e01d0e0f5$d17b4330$7471c990$@gmail.com> <CAPTjJmr0mqxUKzZBS1Ox+8SdmvRmf4nTJNBHa_-QougByJ-juQ@mail.gmail.com>
In-Reply-To <CAPTjJmr0mqxUKzZBS1Ox+8SdmvRmf4nTJNBHa_-QougByJ-juQ@mail.gmail.com>
Subject RE: How to reassign the value of the variable on runtime?
Date Sat, 29 Aug 2015 18:46:19 +0300
MIME-Version 1.0
Content-Type text/plain; charset="us-ascii"
Content-Transfer-Encoding 7bit
X-Mailer Microsoft Outlook 14.0
Thread-Index AQIANVb7qFlJjsnxIgvjO2/smQdhSQEP4jyZnbvnlBA=
Content-Language en-us
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.123.1440863188.11709.python-list@python.org> (permalink)
Lines 221
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1440863188 news.xs4all.nl 23765 [2001:888:2000:d::a6]:57757
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:95753

Show key headers only | View raw



>-----Original Message-----
>From: Python-list
[mailto:python-list-bounces+webmailgroups=gmail.com@python.org] On Behalf Of
Chris Angelico
>Sent: Friday, August 28, 2015 00:03
>Cc: python-list@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@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.  



Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

RE: How to reassign the value of the variable on runtime? "Ivan Evstegneev" <webmailgroups@gmail.com> - 2015-08-29 18:46 +0300

csiph-web