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


Groups > comp.lang.python > #82230

Re: When do default parameters get their values set?

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 "Dave Angel" <dave.angel@1:249/999.remove-cdt-this>
To bSneddon
Subject Re: When do default parameters get their values set?
Message-ID <54866F2E.1768.complang@piz.noip.com> (permalink)
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 <mailman.16740.1418077879.18130.python-list@python.org> a2f3b7a5
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 65
Organization Unlimited download news at news.astraweb.com
NNTP-Posting-Host 2266ad8f.news.astraweb.com
X-Trace DXC=LP`[=a9Aa>5hn]AcMfkkb9L?0kYOcDh@:RScV177L_P1Zh[e@ZS;QS3l23]GVn`Zm6MHo6fPSBkU;04hIVDTD?[;7g\XjYcGeh9fPiGS6_2a\=
Xref csiph.com comp.lang.python:82230

Show key headers only | View raw


  To: bSneddon
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

--- SoupGate-Win32 v1.05
 * Origin: <SpaceSST.BBS.Fidonet<>NNTP.gateway.@.piz.noip.me> (1:249/999)
--- Synchronet 3.15b-Win32 NewsLink 1.92
SpaceSST BBS Usenet <> Fidonet Gateway

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" <bsneddon@1:249/999.remove-cdt-this> - 2014-12-09 11:32 -0500
  Re: When do default parameters get their values set? "Chris Angelico" <chris.angelico@1:249/999.remove-cdt-this> - 2014-12-09 11:32 -0500
  Re: When do default parameters get their values set? "Dave Angel" <dave.angel@1:249/999.remove-cdt-this> - 2014-12-09 11:32 -0500
    Re: When do default parameters get their values set? Duncan Booth <duncan.booth@invalid.invalid> - 2014-12-09 21:18 +0000
      Re: When do default parameters get their values set? random832@fastmail.us - 2014-12-09 18:07 -0500
        Re: When do default parameters get their values set? Rustom Mody <rustompmody@gmail.com> - 2014-12-09 18:44 -0800
          Re: When do default parameters get their values set? random832@fastmail.us - 2014-12-10 13:38 -0500
            Re: When do default parameters get their values set? Rustom Mody <rustompmody@gmail.com> - 2014-12-10 18:18 -0800
              Re: When do default parameters get their values set? Chris Angelico <rosuav@gmail.com> - 2014-12-11 13:24 +1100
                Re: When do default parameters get their values set? Rustom Mody <rustompmody@gmail.com> - 2014-12-10 18:50 -0800
              Re: When do default parameters get their values set? Steven D'Aprano <steve@pearwood.info> - 2014-12-11 03:15 +0000
                Re: When do default parameters get their values set? Rustom Mody <rustompmody@gmail.com> - 2014-12-10 19:26 -0800
                Re: When do default parameters get their values set? Ben Finney <ben+python@benfinney.id.au> - 2014-12-11 14:49 +1100
                Re: When do default parameters get their values set? Chris Kaynor <ckaynor@zindagigames.com> - 2014-12-11 09:10 -0800
                Re: When do default parameters get their values set? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-12-12 09:51 +1100
              Re: When do default parameters get their values set? random832@fastmail.us - 2014-12-11 15:44 -0500
  Re: When do default parameters get their values set? "Tim Chase" <tim.chase@1:249/999.remove-cdt-this> - 2014-12-09 11:32 -0500

csiph-web