All my keys are strings, but I’m considering to use non-alphanumeric characters such as +, :, _, etc as part of the key.
Is there any performance (or any other) impact of using them?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Nope, python dicts will not see any difference in what characters you use in the key string.
They are optimized to deal with strings only versus arbitrary key objects, but if you use only string values for keys you won’t see any difference between using keys without and with non-alphanumeric characters.
The string-only codepath is an optimization for the most common usecase for python dicts: namespaces and function definitions. This is the default mode for a dict; as soon as you store a non-string key in it, the lookup method is switched and you’ll have a teensy-weensy slower dict on your hands. This won’t matter much in most Python applications though.