[Tutor] @property vs @classmethod
alan.gauld at yahoo.co.uk
Sun Jul 9 03:59:16 EDT 2017
On 09/07/17 02:56, Mats Wichmann wrote:
> From OO people, though, we get the sputtering But... But... what about
> encapsulation, data hiding, etc?
Encapsulation isn't really an issue here since Pythons classes
fully encapsulate their data and methods. They just make them
Data Hiding is the issue for some OO purists, especially those
who learned OO via C++/Java. But data hiding was never
originally enforced by the language, when Parnas wrote
his seminal paper there were very few languages that
explicitly supported making module data (he wasn't writing
about OO at the time) private. Instead data hiding was
achieved by providing such a powerful and complete set of
API functions that there was no temptation to play with
the supporting data.
Python adheres to that philosophy, although for "simple"
attributes it does allow, and even encourage, direct access.
But in most cases we, as class designers, should be striving
to provide sufficiently powerful methods that nobody needs
to mess with the internal state directly.
Author of the Learn to Program web site
Follow my photo-blog on Flickr at:
More information about the Tutor