Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newspeer1.nac.net!news.astraweb.com!border5.newsrouter.astraweb.com!not-for-mail From: "Chris Angelico" To: bSneddon Subject: Re: When do default parameters get their values set? Message-ID: <54866F2E.1767.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: 62e3d454 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: 36 Organization: Unlimited download news at news.astraweb.com NNTP-Posting-Host: 2266ad8f.news.astraweb.com X-Trace: DXC=LP`[=a9Aa>e1m]JSMD?cjjL?0kYOcDh@jRScV177L_PaZh[e@ZS;QScl23]GVn`ZmfMHo6fPSBkUk04hIVDTD?[k7g\XjYcGehifPiGS6_2a\m Xref: csiph.com comp.lang.python:82229 To: bSneddon Copy: python-list@python.org (python-list@python.org) On Tue, Dec 9, 2014 at 9:10 AM, 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 They're set when you define the function, and become attributes of the function. If you want to lazily fetch the defaults, here's one common idiom: def printDef(argT=None): """Print the argT value, defaults to cfg['def']""" if argT is None: argT = cfg['def'] print(argT) This depends on None not being a meaningful argument value, of course. If you need to have any object at all able to be passed in, you'd need to create a dedicated sentinel object, or use *args and unpack yourself; but for a lot of cases, None works fine. ChrisA --- 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