[Tutor] Why is it invalid syntax to have a particular dictionary value as an argument?
Mark Lawrence
breamoreboy at yahoo.co.uk
Mon Apr 6 22:08:13 CEST 2015
On 06/04/2015 20:20, Emile van Sebille wrote:
> On 4/6/2015 7:54 AM, boB Stepp wrote:
>> Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit
>> (Intel)] on win32
>> Type "copyright", "credits" or "license()" for more information.
>>>>> d = {'n': 'Print me!'}
>>>>> d
>> {'n': 'Print me!'}
>>>>> d['n']
>> 'Print me!'
>>>>> def func(d['n']):
>> SyntaxError: invalid syntax
>>>>> def func(d):
>> print d['n']
>>
>>>>> func(d)
>> Print me!
>>
>> The plain text does not show it, but in the invalid syntax the "[" is
>> highlighted red.
>>
>> Why is it invalid syntax to pass a particular dictionary value in a
>> function? Or does it require a different form to do so?
>
> Maybe this form helps:
>
> 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
>
> Emile
>
Dreadful advice. There have always have been and always will be
questions from newbies as they do not understand how this construct
works in Python. Please *DO NOT* put such things forward.
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence
More information about the Tutor
mailing list