Posts filed under ‘encapsulation’
Adventures in Pythonic Encapsulation
Python has undergone a fair share of criticism for its lack of support for information hiding. Despite its being a solidly object-oriented language, Python has historically refused to support this facet of object-orientation. Other programming languages have implemented encapsulation in a variety of ways:
- All variables in Smalltalk, the canonical OO language, are private; in order to access them, the programmer must write explicit accessor and mutator methods for each variable.
- C++, Java, and C# rely on the
public,private, andprotectedkeywords in order to implement variable scoping and encapsulation. - Cocoa’s Objective-C code strongly encourages the programmer to use key-value coding for encapsulatory purposes.
- Ruby does ultra-sexy encapsulation through the
attr_accessorfunction(s).
However, Pythonistas like myself often assert that “we’re all consenting adults here.” While this attitude is refreshing in this day of slavish devotion to OOP principles, the Python community has realized that in order to avoid alienating newcomers, Python should perform some sort of encapsulatory behavior. This article intends to show the various ways in which Python supports encapsulation and/or information hiding.