How ' dict ' word used as a variable in Python? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How ' dict ' word used as a variable in Python?

dict = {1:'hello', 2:'everyone'} print(dict[1]) # it displays hello here Now, print(type(dict)) #displays <class 'dict'> My question is how the class keyword can be used as variable. is it possible to use like all the others as variables in python?

19th Jul 2021, 5:02 AM
Vinitha
Vinitha - avatar
2 Answers
+ 5
Vinitha , yes it can be used as variable names, unfortunately there is no warning that this can create problems. in general it is NOT recommended to use names from classes, build-in functions or other stuff and use them as variable names. this can lead to a unexpected behavior and creates errors that are difficult to identify. so don't use: str, int, sum, ... as variable names if you like these names, you can add a "_" at the end like sum_ = ... happy coding!
19th Jul 2021, 3:42 PM
Lothar
Lothar - avatar
+ 4
Basically, dict class has been written already during the python language development and is abstracted from the user ( meaning not very important to show the it's implementation to the user/programmer) . If you hate in-built python dict then you can even write your own dict data structure using any language that you want(using python itself). For more detailed answer, https://stackoverflow.com/questions/327311/how-are-pythons-built-in-dictionaries-implemented
19th Jul 2021, 5:09 AM
Rohit