[Tutor] Why is it invalid syntax to have a particular dictionary value as an argument?
Emile van Sebille
emile at fenx.com
Tue Apr 7 17:57:16 CEST 2015
On 4/6/2015 12:42 PM, Dave Angel wrote:
> On 04/06/2015 03:20 PM, Emile van Sebille wrote:
<snip>
>> Python 2.7.6 (default, Mar 22 2014, 22:59:56)
>> [GCC 4.8.2] on linux2
>> Type "help", "copyright", "credits" or "license" for more information.
>> >>> d = {'a':'123'}
>> >>> def func(s=d['a']):
>> ... print s
>> ...
>> >>> func()
>> 123
>>
>
> Only if you know that nobody is going to be changing d.
Clearly my example avoids the pitfalls of a changing d. :)
> >>> d = {"a":"123"}
> >>> def func(s=d["a"]):
> ... print s
> ...
> >>> d["a"] = "new value"
> >>> func()
> 123
Good point -- I forgot about setting default parameters at compile time.
>>> d={'a':'123'}
>>> def func(s=d):print s['a']
...
>>> func()
123
>>>
>>> d['a']='456'
>>> func()
456
>>>
More information about the Tutor
mailing list