/ Design Patterns / Structural patterns / Private Class Data Private Class Data Design Pattern in Python Back to Private Class Data description """ Control write access to class attributes. Separate data from methods that use it. Encapsulate class data initialization. """ class DataClass: """ Hide all the attributes. """ def __init__(self): self.value = None def __get__(self, instance, owner): return self.value def __set__(self, instance, value): if self.value is None: self.value = value class MainClass: """ Initialize data class through the data class's constructor. """ attribute = DataClass() def __init__(self, value): self.attribute = value def main(): m = MainClass(True) m.attribute = False if __name__ == "__main__": main() Support our free website and own the eBook! 22 design patterns and 8 principles explained in depth 406 well-structured, easy to read, jargon-free pages 228 clear and helpful illustrations and diagrams An archive with code examples in 4 languages All devices supported: EPUB/MOBI/PDF formats Learn more...