Path: csiph.com!usenet.pasdenom.info!usenet.blueworldhosting.com!feeder01.blueworldhosting.com!news.alt.net!news.astraweb.com!border5.newsrouter.astraweb.com!not-for-mail From: "Tim Chase" To: bSneddon Subject: Re: When do default parameters get their values set? Message-ID: <54866F2E.1769.complang@piz.noip.com> X-Comment-To: bSneddon Newsgroups: comp.lang.python In-Reply-To: <54866F2E.1765.complang@piz.noip.com> References: <54866F2E.1765.complang@piz.noip.com> X-FTN-AREA: COMP.LANG.PYTHON X-FTN-PID: SoupGate-Win32 v1.05 X-FTN-MSGID: 5be4f19a X-FTN-REPLY: <8c10ecec-c057-4580-9711-94c0a5151646@googlegroups.com> bdfc525e Content-Type: text/plain; charset=IBM437 Content-Transfer-Encoding: 8bit X-Gateway: piz.noip.com [Synchronet 3.15b-Win32 NewsLink 1.92] X-Server: piz.noip.me Date: 9 Dec 2014 11:32:01 -0500 Lines: 45 Organization: Unlimited download news at news.astraweb.com NNTP-Posting-Host: 2266ad8f.news.astraweb.com X-Trace: DXC=LP`[=a9Aa>ElVZHQGRZg_EL?0kYOcDh@JRScV177L_PAZh[e@ZS;QSCl23]GVn`ZmFMHo6fPSBkUK04hIVDTD?[K7g\XjYcGehIfPiGS6_2a\M Xref: csiph.com comp.lang.python:82231 To: bSneddon Copy: python-list@python.org On 2014-12-08 14:10, 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']): At this point (after the "def" has completed defining the function), the expression is evaluated and assigned to the default argument. > 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? Well, you can bind to a default dictionary rather than an entry in that dictionary: cfg = {'def': 'blue'} def printDef(config=cfg): ... access(config['def']) which will make the look-up happen at run-time rather than definition/bind-time. beTest.cfg['def'] = 'Red!' beTest.printDef() # should print "Red!" -tkc --- SoupGate-Win32 v1.05 * Origin: NNTP.gateway.@.piz.noip.me> (1:249/999) --- Synchronet 3.15b-Win32 NewsLink 1.92 SpaceSST BBS Usenet <> Fidonet Gateway