Non-identifiers in dictionary keys for **expression syntax

M

Matthew Gilson

This is a question regarding the documentation around dictionary
unpacking. The documentation for the call syntax
(http://docs.python.org/3/reference/expressions.html#grammar-token-call)
says:

"If the syntax **expression appears in the function call, expression
must evaluate to a mapping, the contents of which are treated as
additional keyword arguments."

That's fine, but what is a keyword argument? According to the glossary
(http://docs.python.org/3.3/glossary.html):

/"keyword argument/: an argument preceded by an identifier (e.g. name=)
in a function call or passed as a value in a dictionary preceded by **."

As far as I'm concerned, this leads to some ambiguity in whether the
keys of the mapping need to be valid identifiers or not.

Using Cpython, we can do the following:

def func(**kwargs):
print kwargs

d = {'foo bar baz':3}

So that might lead us to believe that the keys of the mapping do not
need to be valid identifiers. However, the previous function does not
work with the following dictionary:

d = {1:3}

because not all the keys are strings. Is there a way to petition to get
this more rigorously defined?

Thanks,
~Matt
 
N

Neil Cerutti

That's fine, but what is a keyword argument? According to the glossary
(http://docs.python.org/3.3/glossary.html):

/"keyword argument/: an argument preceded by an identifier (e.g. name=)
in a function call or passed as a value in a dictionary preceded by **."

As far as I'm concerned, this leads to some ambiguity in
whether the keys of the mapping need to be valid identifiers or
not.

I don't see any ambiguity. A keyword argument is an argument
preceded by an identifier according to the definition. Where are
you perceiving wiggle room?
 
E

Ethan Furman

I don't see any ambiguity. A keyword argument is an argument
preceded by an identifier according to the definition. Where are
you perceiving wiggle room?

--> def func(**kwargs):
.... print(kwargs)
....

--> d = {'foo bar baz':3}

--> func(**d)
{'foo bar baz': 3}

Even though 'foo bar baz' is not a valid identifier, and could not be passed as `func(foo bar baz = 3)`, it still worked
when going through a dict.
 
M

Matthew Gilson

I don't see any ambiguity. A keyword argument is an argument
preceded by an identifier according to the definition. Where are
you perceiving wiggle room?
The wiggle room comes from the "or passed as a value in a dictionary"
clause. We sort of get caught in a infinite loop there because the
stuff that can be passed in a dictionary is a keyword which is an
identifer=expression or something passed as a value in a dictionary ...

Also the fact that:

func(**{"foo bar baz":1})

works even though `foo bar baz` isn't a valid identifier, but:

func(**{1:3})

doesn't work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top