oop - How to make a class field [list] read-only in python? -


I have self.some_field = [] in my class. I have a way to read this list - only one Like the property?

You definitely need to make a property ... like, __init__

  self._some_field = []  

and then later in the class body:

  @ Property def some_field (self): return self._some_field  

Note that this no list makes itself invariant: what will fail

  self.some_field = 'bah'  

no is a mutator Calls like, say, If you want to make the area irreversible, then this list is not included, the definition (

Since there is a changeable sequence in a list) - this should be some other type of sequence (an irreversible) that you have to define for the purpose.


Comments