AnalysisToolbox icon indicating copy to clipboard operation
AnalysisToolbox copied to clipboard

constants class?

Open clarkedavida opened this issue 3 years ago • 1 comments

If you are not careful with lists, you can accidentally change variables without meaning to through shallow copies. Is there a way to implement a constants class that can't be accidentally modified?

Maybe something like this: https://discuss.dizzycoding.com/can-i-prevent-modifying-an-object-in-python/

clarkedavida avatar Dec 10 '22 01:12 clarkedavida

I do that usually like this:

from typing import NamedTuple
class test(NamedTuple):
    blub: int
    bla: float

this_test = test(4, 3.2)
this_test.blub = 5  # this will fail, because it's immutable.

This can be used to pass one immutable "parameter object" to a function, instead of passing lots of single parameter which are then also mutable.

luhuhis avatar Dec 10 '22 09:12 luhuhis