Note This is not a duplicate of the linked answer , which focuses on performance issues and what happens behind the curtains when the dict () function is called. My question is that keyword arguments always result 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. I like to say python to give me a dictionary where are the lines and is the number . But in method-2, how does python know that keys are strings, and not something else?
Is this a trend? if so, then some other examples (except dictionaries) that show this peculiar behavior?
EDIT-1 :
My understanding of the answers:
- Method-2 is a way to create a
dict(**kwargs) dictionary dict(**kwargs) . - In
spades=3 , spades is a valid Python identifier, so it is taken as a string key.
So, will dict(**kwargs) always be the result in a dictionary where the keys are of type string ?
EDIT-2 : ^^ YES.
source share