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 :
- Method-2 is the
dict(**kwargs)
way of creating a dictionary. - In
spades=3
,spades
is a valid Python identifier, so it is taken as a key of typestring
.
So, will dict(**kwargs)
always result in a dictionary where the keys are of type string
?
EDIT-2 : ^^ YES.