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


Groups > comp.lang.python > #95754

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

Path csiph.com!news.mixmin.net!newsreader4.netcologne.de!news.netcologne.de!takemy.news.telefonica.de!telefonica.de!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'none,': 0.05; 'none:': 0.05; 'correct.': 0.07; 'executed': 0.07; 'override': 0.07; 'cc:addr:python-list': 0.09; 'subject:How': 0.09; 'assumed': 0.09; 'called.': 0.09; 'globals': 0.09; 'none.': 0.09; 'python': 0.10; 'def': 0.13; 'argument': 0.15; 'arguments:': 0.16; 'arguments;': 0.16; 'called,': 0.16; 'defaults,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'function?': 0.16; 'globals.': 0.16; 'ivan': 0.16; 'scope.': 0.16; 'subject:variable': 0.16; 'then?': 0.16; 'wrote:': 0.16; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'aug': 0.20; 'suggested': 0.20; 'function,': 0.22; 'skip:* 50': 0.22; 'am,': 0.23; 'defined': 0.23; 'replacing': 0.23; 'header:In-Reply-To:1': 0.24; 'module': 0.25; '(which': 0.26; 'skip:" 20': 0.26; 'define': 0.27; 'right.': 0.27; 'message-id:@mail.gmail.com': 0.27; 'object,': 0.27; 'skip:e 30': 0.27; 'function': 0.28; 'values': 0.28; 'looks': 0.29; 'correct,': 0.29; 'objects': 0.29; 'that.': 0.30; "can't": 0.32; 'statement': 0.32; 'right?': 0.33; 'changing': 0.34; 'received:google.com': 0.35; 'skip:c 30': 0.35; 'but': 0.36; 'cases': 0.36; 'form,': 0.36; 'loaded': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'say': 0.37; 'difference': 0.38; "won't": 0.38; 'means': 0.39; 'subject:the': 0.39; 'rather': 0.39; 'still': 0.40; 'save': 0.60; 'behavior': 0.61; "you'll": 0.61; 'default': 0.61; 'engine': 0.62; '30,': 0.63; 'within': 0.64; 'grab': 0.64; 'between': 0.65; 'direct': 0.68; 'evaluate': 0.72; 'special': 0.73; 'directly.': 0.76; '"hey,': 0.84; "'def'": 0.84; 'chrisa': 0.84; 'snapshots': 0.84; 'subject:value': 0.84; 'to:none': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=peVi4BDmVglmcUeMK9odZf7n8kw9EbFMXM21Vr8CDIo=; b=BN8Kjzec/SP7mppFncGsOhXOgman273UgiifkDQloY6Z6/iup041kasnhf4YIR0mhF F/q4ErSm0TSWNL/UpP+46L5l9UAif4fukMYm7ogYtj4ChpiFZHNozf0nngyOmfzdC+Wr +GV8YY5DABuHpbFeVwN9hZac6OXKUbVgXIZ2gt8am4AKVmQoCooVyxDm5okk6oMmHfkH a5oFwLg8pFpQmWyahjvl7L8q2rc7DD1r2nVbEs8tHoeC72h1SU5J8wGGsnihwr6M6K6L DVVZTsDYlZWAG92p93FySbT3Uf3a9KAN+mRRdphFOc0iG3h5skfQ/UmKrZruCooieN66 3oxA==
MIME-Version 1.0
X-Received by 10.50.138.72 with SMTP id qo8mr4810741igb.92.1440864124356; Sat, 29 Aug 2015 09:02:04 -0700 (PDT)
In-Reply-To <000c01d0e271$d9c9b080$8d5d1180$@gmail.com>
References <002e01d0e0f5$d17b4330$7471c990$@gmail.com> <CAPTjJmr0mqxUKzZBS1Ox+8SdmvRmf4nTJNBHa_-QougByJ-juQ@mail.gmail.com> <000c01d0e271$d9c9b080$8d5d1180$@gmail.com>
Date Sun, 30 Aug 2015 02:02:04 +1000
Subject Re: How to reassign the value of the variable on runtime?
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
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.124.1440864128.11709.python-list@python.org> (permalink)
Lines 55
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1440864128 news.xs4all.nl 23769 [2001:888:2000:d::a6]:60908
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:95754

Show key headers only | View raw


On Sun, Aug 30, 2015 at 1:46 AM, Ivan Evstegneev
<webmailgroups@gmail.com> wrote:
> 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.

That's correct. When you define a function, it snapshots the default
values for its arguments; if you say "engine=engine_object", Python
will evaluate the name "engine_object" at the time when the 'def'
statement is executed (which in this case is module load) and save
that as the default value for the argument "engine". Changing the
value of engine_object won't change that.

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

Right. If you use "engine=None", then Python will evaluate the
expression "None", which (what a surprise!) means the special object
None. There's only one such object, and you'll never be replacing it.
Then, whenever the function is called, you say "hey, if this thing's
None, go and grab the one from the global"; and since that happens
when the function's _called_ rather than when it's _defined_, it uses
the value of engine_object at that time.

> 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?

Correct, both will be fetched from the outer scope. The difference is
that you can override them using the first form, by calling the
function with arguments:

current_data_export(some_other_engine, some_other_meta)

which you can't do if you use the globals directly. But both forms do
use the current values of the globals as at when the function is
called.

ChrisA

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


Thread

Re: How to reassign the value of the variable on runtime? Chris Angelico <rosuav@gmail.com> - 2015-08-30 02:02 +1000

csiph-web