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


Groups > comp.lang.python > #82154

Re: When do default parameters get their values set?

Date 2014-12-08 16:26 -0600
From Tim Chase <python.list@tim.thechases.com>
Subject Re: When do default parameters get their values set?
References <8c10ecec-c057-4580-9711-94c0a5151646@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.16741.1418078546.18130.python-list@python.org> (permalink)

Show all headers | View raw


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


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