7

Note : This is not a duplicate of the linked answer, that focuses on issues related to performance, and what happens behind the curtains when a dict() function call is made. My Question is about keyword arguments always resulting in keys of type string. Definitely not a duplicate.


Method-1 :

suit_values = {'spades':3, 'hearts':2, 'diamonds':1, 'clubs':0}

Method-2 :

suit_values = dict(spades=3, hearts=2, diamonds=1, clubs=0)

Method-1 makes sense to me. It's like telling python to give me a dictionary where keys are strings and values are numerics. But in Method-2, how does python know that the keys are strings and not something else ?

Is this a trend? if so, some other examples (apart from dictionaries) that show this kinda behavior ?

EDIT-1 :

My understanding from the answers is :

  1. Method-2 is the dict(**kwargs) way of creating a dictionary.
  2. In spades=3, spades is a valid Python identifier, so it is taken as a key of type string .

So, will dict(**kwargs) always result in a dictionary where the keys are of type string ?

EDIT-2 : ^^ YES.

Community
  • 1
  • 1
Somjit
  • 2,503
  • 5
  • 33
  • 60

2 Answers2

4

In the second case, the dict function accepts keyword arguments. And the keyword arguments can only be passed as string parameters.

Quoting the documentation,

Providing keyword arguments as in the first example only works for keys that are valid Python identifiers. Otherwise, any valid keys can be used.

As long as the string is a valid python identifier, you can used that as a key in the second form. For example, the following will not work with the second form

>>> dict(1=2)
  File "<input>", line 1
SyntaxError: keyword can't be an expression

But the same will work with the first form

>>> {1:2}
{1: 2}
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
  • i didn't really get the " And the keyword arguments can only be passed with strings as parameters " part. – Somjit Oct 26 '15 at 06:28
  • @Somjit Hmmm, I edited a little bit. Hope it is clear now. – thefourtheye Oct 26 '15 at 06:30
  • Ok. That makes sense. So keyword arguments can be passed as strings only. But after thay are passed, they are also 'absorbed' as strings, ie, stored as string type objects ? My edit asks that question at the end. – Somjit Oct 26 '15 at 06:33
  • @Somjit To answer your last question, answer is yes. When you use keyword args, the keys will be strings only. – thefourtheye Oct 26 '15 at 06:35
  • @Somjit: When you use `keyword=value` args with _any_ function those keywords get converted into string keys in a `dict`. The keywords **have to** be of a form that's legal to use as a Python identifier since they get promoted to strings by the same mechanism that creates the keys that are used as variable names, eg in a function's `locals()` dictionary. – PM 2Ring Oct 26 '15 at 10:54
2

Method 1, a literal dictionary expression, can use as keys any hashable value, like strings, integers, or tuples.

Method 2 - the dict() built-in function - doesn't "know" that the keys are strings. Whatever keys you use with that method have to follow traditional variable naming conventions, to be converted into strings. That means that each key must start with a letter or underscore, contain only letters, underscores, or numbers, and so on. Trying a different key, like 2a or (1,2), will fail.

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
  • can you please reopen the post. It is not the same as the question you marked as being duplicate of. – Somjit Oct 26 '15 at 10:23