+ 1

Is it a bad practice to use "list" as a class-variable?

Example: class Entity: list = [] def __init__(self): Entity.list.append(self) It shouldn't overwrite anything since the function list belongs to python but not to the class Entity. Is it still a bad practice to do this?

25th Apr 2019, 7:46 PM
Lumine
Lumine - avatar
2 Respostas
+ 1
It does overwrite the function, just not on all scopes. The following program throws a TypeError. class Entity: list = [] def __init__(self): Entity.list.append(self) print(list("Hello world!")) It may see harmless at first, but for more complex codes it's a really big deal.
25th Apr 2019, 10:12 PM
Diego
Diego - avatar
+ 1
Yes
30th Jul 2020, 12:07 PM
Rakshit Pahel
Rakshit Pahel - avatar