Isnt it bad style to call a list variable "list" ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Isnt it bad style to call a list variable "list" ?

I think it might cause errors. But challanges are full of that.

17th Dec 2018, 6:29 PM
survivor
3 Answers
+ 8
It is bad style and it can seriously damage your code. For example, you can use "neutral" variable names like this: a = [1, 2, 3] b = list((1, 2, 3)) print(a) # [1, 2, 3] print(b) # [1, 2, 3] If you use "list" as a variable name, this happens: list = [1, 2, 3] b = list((1, 2, 3)) # TypeError, built-in list() method can't be accessed anymore because "list" is now your variable
17th Dec 2018, 6:51 PM
Anna
Anna - avatar
+ 4
You temporarily use the name 'list' for something different than lists. You could do list=print, and then list('Hello world.') And as long as list is used for print, you won't be able to use lists. (You can 'free' lists with del list.) Unless... ... how about list, print = print, list? :) Yeah, better not to do this at all imo.
17th Dec 2018, 6:38 PM
HonFu
HonFu - avatar
+ 1
thank you Anna it is a good example.
17th Dec 2018, 6:53 PM
survivor