Search code examples
pythonoopclassstatic

python: immutable private class variables?


Is there any way to translate this Java code into Python?

class Foo
{
    final static private List<Thingy> thingies = 
       ImmutableList.of(thing1, thing2, thing3);
}

e.g. thingies is an immutable private list of Thingy objects that belongs to the Foo class rather than its instance.

I know how to define static class variables from this question Static class variables in Python but I don't know how to make them immutable and private.


Solution

  • You can't do either of those things in Python, not in the sense you do them in Java, anyway.

    By convention, names prefixed with an underscore are considered private and should not be accessed outside the implementation, but nothing in Python enforces this convention. It's considered more of a warning that you're messing with an implementation detail that may change without warning in a future version of the code.