[Tutor] Why are expressions not allowed as parameters in function definition statements?

Alan Gauld alan.gauld at yahoo.co.uk
Sat Jun 18 20:14:44 EDT 2016


On 18/06/16 20:04, boB Stepp wrote:

> py3: def d(row, col/2, radius=5):
>   File "<stdin>", line 1
>     def d(row, col/2, radius=5):
>                   ^
> SyntaxError: invalid syntax
> 
> And this surprised me.  It seems that only identifiers are allowed as
> parameters in a function definition statement, and I cannot help but
> wonder why?  

What are the parameters (as opposed to the arguments)?
They are names. They are keys in the local dictionary
used by the function to look up the values passed in
as arguments.

You cannot use an expression as a key in a dictionary.
If you try, Python tries to evaluate the expression
and uses the result if its valid or throws a nameerror etc.

hth
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list