I was playing around with the risks of class-level variables in Python, and I thought that the risk of lists as class-level variables can be solved with tuples, for example, an empty tuple.
Take:
class BaseClass(object):
default_sth = []
def foo(self):
for sth in self.default_sth:
pass
This is risky because:
class SubClass(BaseClass):
pass
a = SubClass()
a.default_sth.append(3)
print(BaseClass.default_sth) # [3]
Using None
instead of empty tuple (tuple()
?) could be instead dangerous because it fails when iterating over it.
Because of immutability, I think this is a good option, but I don't think I've seen anybody talk about this around anywhere. Do you see any flaws with this reasoning? Is it pythonic?