Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: 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; 'value,': 0.04; 'argument': 0.05; 'suppose': 0.07; 'variables': 0.07; 'here?': 0.09; 'none):': 0.09; 'subject:set': 0.09; 'def': 0.12; 'gui': 0.12; "(i'm": 0.16; 'called,': 0.16; 'defined,': 0.16; 'dictionary.': 0.16; 'imports': 0.16; 'overridden': 0.16; 'sentinel': 0.16; 'subject:default': 0.16; 'subject:parameters': 0.16; 'subject:their': 0.16; 'subject:values': 0.16; 'wrote:': 0.18; 'obviously': 0.18; 'module': 0.19; 'value.': 0.19; 'import': 0.22; 'email addr:gmail.com>': 0.22; 'to:name:python-list@python.org': 0.22; 'print': 0.22; 'mon,': 0.24; 'pass': 0.26; 'values': 0.27; 'header :In-Reply-To:1': 0.27; 'function': 0.29; 'dec': 0.30; 'message- id:@mail.gmail.com': 0.30; 'default,': 0.31; 'prints': 0.31; 'another': 0.32; 'call.': 0.33; 'received:74.125.82': 0.34; 'could': 0.34; 'problem.': 0.35; 'case,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'false': 0.36; 'module.': 0.36; 'useful': 0.36; 'subject:?': 0.36; 'changing': 0.37; 'wrong': 0.37; 'skip:& 10': 0.38; 'to:addr:python-list': 0.38; 'issue': 0.38; 'pm,': 0.38; 'received:74.125': 0.39; 'to:addr:python.org': 0.39; 'simple': 0.61; 'back': 0.62; 'name': 0.63; 'more': 0.64; 'here': 0.66; 'default': 0.69; 'subject:get': 0.81 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:content-type; bh=so/NQwCRxK86w2tklsXgl79GCH2ezh2OuhLbcMdqh0E=; b=iOOQ09coSNOxAcm6zHY3kcY5JmiSnjt7p7EKelSi2jcnpMx6lPUOH4OhKszVQiWJeX r8m+92/RX61LIj/1fwfjjOddJ8TAyD/cyGf2SHdQQtfF3RqOunbWV7ZYaiW4gLX+1aAY 6MA/SmoqpI2tj6mjZ8wC+CF1wPpk5ZDPVIHCSEXjrTU6w4CTqUK3m4zzNQ6Jw60QYUWe skbs1/y1dd7qqaDUN8K+zJBHgK80JU/eQaAqmy5drUgyS88AHAKpHjUSaqDRU4baCN1N rRde2bTT2cUtHjwkFq/b3DzLgeP/HQ1FTt1GBSZ2giNP0QIHebwSUdWIBXFXGGHonAoB TJ+A== X-Gm-Message-State: ALoCoQmdLFPQNVyGgDpZZPeipS3L98mjcN0SaprKa4Mw13XPKvyTk5CkBDbvWsgF88aeVw3RId3W X-Received: by 10.194.119.99 with SMTP id kt3mr49927912wjb.14.1418077718622; Mon, 08 Dec 2014 14:28:38 -0800 (PST) MIME-Version: 1.0 In-Reply-To: <8c10ecec-c057-4580-9711-94c0a5151646@googlegroups.com> References: <8c10ecec-c057-4580-9711-94c0a5151646@googlegroups.com> From: Chris Kaynor Date: Mon, 8 Dec 2014 14:28:18 -0800 Subject: Re: When do default parameters get their values set? To: "python-list@python.org" Content-Type: multipart/alternative; boundary=089e01227b0eaec9e70509bbf2fa X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 101 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1418077725 news.xs4all.nl 2833 [2001:888:2000:d::a6]:52211 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:82152 --089e01227b0eaec9e70509bbf2fa Content-Type: text/plain; charset=UTF-8 On Mon, Dec 8, 2014 at 2: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. > > #module name beTest.py > > cfg = { 'def' : 'blue'} > > def printDef(argT = cfg['def']): print argT Here is your problem. The default argument value is evaulated at the time the function is defined, not when it is called, so argT will boung to 'blue' by default, unless overridden in the call. In this simple case, you could just change the function to: def printDef(): print cfg['def'] In more complicated cases, where you actually need to pass in the argument with a default, the best way is to use a sentinel value (I'm using None in the case): def printDef(argT = None): argT = argT or cfg['def'] # If argT could be False or another "falsy" value, use "argT = argT if argT is not None else cfg['def']". This is also useful if None needs to be a valid value. 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? --089e01227b0eaec9e70509bbf2fa Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
On M= on, Dec 8, 2014 at 2:10 PM, bSneddon <w.g.sneddon@gmail.com> wrote:
I ran into an issue setting variables from = a GUI module that imports a back end module.=C2=A0 My approach was wrong ob= viously but what is the best way to set values in a back end module.

#module name beTest.py

cfg =3D { 'def' : 'blue'}

def printDef(argT =3D cfg['def']):
=C2=A0 =C2=A0 =C2=A0 =C2=A0 print argT

Here= is your problem. The default argument value is evaulated at the time the f= unction is defined, not when it is called, so argT will boung to 'blue&= #39; by default, unless overridden in the call.
In this simple ca= se, you could just change the function to:

def pri= ntDef():
=C2=A0 =C2=A0 print cfg['def']

In more complicated cases, where you actually need to pass in the a= rgument with a default, the best way is to use a sentinel value (I'm us= ing None in the case):

def printDef(argT =3D None)= :
=C2=A0 =C2=A0 argT =3D argT or cfg['def'] # If argT cou= ld be False or another "falsy" value, use "argT =3D argT if = argT is not None else cfg['def']". This is also useful if None= needs to be a valid value.
=C2=A0 =C2=A0 print argT
= =C2=A0


#module name feTest
import beTest

beTest.cfg['def'] =3D "no red"
beTest.printDef()



This prints blue.=C2=A0 =C2=A0 =C2=A0 I suppose because I am changing a loc= al copy of cfg dictionary.=C2=A0 What is the write approach here?
--089e01227b0eaec9e70509bbf2fa--