Attribute Design Patterns

Programmers coming from other languages (particularly Java and C++) can try to make all attributes private and write extensive getter and setter functions. This kind of design pattern can be necessary for languages where type definitions are statically compiled into the runtime. It is not necessary in Python. Python depends on a different set of common patterns.

In Python, it's common to treat all attributes as public. This means the following:

  • All attributes should be well documented.
  • Attributes should properly reflect the state of the object; they shouldn't be temporary or transient values.
  • In the rare case of an attribute that has a potentially confusing (or brittle) value, a single leading underscore character (_) marks the name as not part of the defined interface. It's not technically private, but it can't be relied on in the next release of the framework or package.

It's important to think of private attributes as a nuisance. Encapsulation isn't broken by the lack of complex privacy mechanisms in the language; proper encapsulation can only be broken by bad design.

Additionally, we have to choose between an attribute or a property which has the same syntax as an attribute, but can have more complex semantics. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset