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


Groups > comp.lang.python > #82153

Re: When do default parameters get their values set?

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <davea@davea.name>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.007
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'none:': 0.07; 'suppose': 0.07; 'variables': 0.07; 'string': 0.09; 'here?': 0.09; 'immutable': 0.09; 'lookup': 0.09; 'subject:set': 0.09; 'def': 0.12; 'gui': 0.12; 'stored': 0.12; 'dictionary,': 0.16; 'dictionary.': 0.16; 'immutable,': 0.16; 'imports': 0.16; 'simplified': 0.16; 'subject:default': 0.16; 'subject:parameters': 0.16; 'subject:their': 0.16; 'subject:values': 0.16; 'values:': 0.16; 'wrote:': 0.18; 'obviously': 0.18; 'module': 0.19; 'import': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'fairly': 0.24; "haven't": 0.24; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'evaluation': 0.30; 'lines': 0.31; 'prints': 0.31; 'run': 0.32; 'common': 0.35; 'but': 0.35; 'there': 0.35; 'are,': 0.36; 'module.': 0.36; 'subject:?': 0.36; 'changing': 0.37; 'wrong': 0.37; 'two': 0.37; 'being': 0.38; 'depends': 0.38; 'to:addr:python-list': 0.38; 'issue': 0.38; 'pm,': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'even': 0.60; "you're": 0.61; 'back': 0.62; 'making': 0.63; 'name': 0.63; 'more': 0.64; 'line,': 0.68; 'received:74.208': 0.68; 'subject': 0.69; 'default': 0.69; 'stated': 0.69; 'goal': 0.75; 'subject:get': 0.81; 'idiom': 0.84; 'received:74.208.4.194': 0.84; 'symptoms': 0.84
Date Mon, 08 Dec 2014 17:30:57 -0500
From Dave Angel <davea@davea.name>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.4.0
MIME-Version 1.0
To python-list@python.org
Subject Re: When do default parameters get their values set?
References <8c10ecec-c057-4580-9711-94c0a5151646@googlegroups.com>
In-Reply-To <8c10ecec-c057-4580-9711-94c0a5151646@googlegroups.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Provags-ID V02:K0:g9Fun78rRA2mOwsbpbg4ccyOYexIFjxAGH/UF9Y3elP BbvAGIT3JBscblLANIWZifjgK6Iyp5gNQvFoX0FUMezqcGKesn rL8+451OjoF1hTjj8/UFupk+3n6ov/QUyBRqtCg+daYkP2sBYS MWp/FL+83TfY9/jZg201Yde8KUdoS/x1d6t1D+BOd25u0cDOeC 8lvviEGg/olIvEpbNdFKTh23xTcLRMlgnf63Ur/hMnSRQopquF syonFI+uLMZmyK174IHQyu7NvK/2v0STt8CggADb2uoo6VLCqI Q7LtfKRnBXnvuG7KFtz9pozAXUggk92T1/q+PQmfzjCqKQE4pQ NKHBd/Ip5lIOIdTXJt/8=
X-UI-Out-Filterresults notjunk:1;
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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.16740.1418077879.18130.python-list@python.org> (permalink)
Lines 57
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1418077879 news.xs4all.nl 2937 [2001:888:2000:d::a6]:55369
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:82153

Show key headers only | View raw


On 12/08/2014 05:10 PM, bSneddon wrote:
> I ran into an issue setting variables from a GUI module that imports a back end module.  My approach was wrong obviously but what is the best way to set values in a back end module.
>

To answer the subject line, the default parameter(s) are evaluated when 
the function is compiled, and then stored with the function.  So in this 
case the default for argT is an immutable string "blue"  Being 
immutable, nothing will change that for the run of the program.

> #module name beTest.py
>
> cfg = { 'def' : 'blue'}
>
> def printDef(argT = cfg['def']):
> 	print argT
>
>
> #module name feTest
> import beTest
>
> beTest.cfg['def'] = "no red"
> beTest.printDef()
>
>
>
> This prints blue.      I suppose because I am changing a local copy of cfg dictionary.  What is the write approach here?
>

You're not making a local copy of any dictionary.  The symptoms you have 
would be identical even if you run beTest.py directly (with those two 
lines added, of course).

The RIGHT approach depends on what your goal is here.  Obviously this is 
simplified from some more complex use, but you haven't stated what your 
desires are, so I don't necessarily know how to achieve them.

There is only one dictionary, and it does change when you you say:
    beTest.cfg['def'] = "no red"

But if you need to do a lookup in that dictionary upon calling 
primtDef(), then you're going to make the evaluation at that time, not 
at default-time.

How about the following, fairly common idiom for default values:

def printDef(argT = None]):
     if argT = None:
         argT = cfg['def']
     print argT





-- 
DaveA

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


Thread

When do default parameters get their values set? bSneddon <w.g.sneddon@gmail.com> - 2014-12-08 14:10 -0800
  Re: When do default parameters get their values set? Chris Angelico <rosuav@gmail.com> - 2014-12-09 09:28 +1100
  Re: When do default parameters get their values set? Chris Kaynor <ckaynor@zindagigames.com> - 2014-12-08 14:28 -0800
  Re: When do default parameters get their values set? Dave Angel <davea@davea.name> - 2014-12-08 17:30 -0500
  Re: When do default parameters get their values set? Tim Chase <python.list@tim.thechases.com> - 2014-12-08 16:26 -0600
  Re: When do default parameters get their values set? bSneddon <w.g.sneddon@gmail.com> - 2014-12-08 15:35 -0800
  Re: When do default parameters get their values set? Terry Reedy <tjreedy@udel.edu> - 2014-12-08 20:37 -0500

csiph-web