So let's say I wanna make a dictionary. We'll call it d
. But there are multiple ways to initialize a dictionary in Python! For example, I could do this:
d = {'hash': 'bang', 'slash': 'dot'}
Or I could do this:
d = dict(hash='bang', slash='dot')
Or this, curiously:
d = dict({'hash': 'bang', 'slash': 'dot'})
Or this:
d = dict([['hash', 'bang'], ['slash', 'dot']])
And a whole other multitude of ways with the dict()
function. So obviously one of the things dict()
provides is flexibility in syntax and initialization. But that's not what I'm asking about.
Say I were to make d
just an empty dictionary. What goes on behind the scenes of the Python interpreter when I do d = {}
versus d = dict()
? Is it simply two ways to do the same thing? Does using {}
have the additional call of dict()
? Does one have (even negligible) more overhead than the other? While the question is really completely unimportant, it's a curiosity I would love to have answered.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…