[Tutor] @property vs @classmethod
Steven D'Aprano
steve at pearwood.info
Sun Jul 9 07:02:49 EDT 2017
On Sat, Jul 08, 2017 at 07:56:10PM -0600, Mats Wichmann wrote:
> From OO people, though, we get the sputtering But... But... what about
> encapsulation, data hiding, etc?
Regular attributes are still encapsulated. There's no difference in
encapsulation between:
obj.x # a standard attribute
obj.x # a property
obj.getx() # a Java-like getter
Whichever way you choose, x is still attached to the instance.
Data hiding is a good point, for those who care about data hiding. But
Python has an answer to that too: we can hide data by prefixing the name
with a single underscore.
obj._x
Or at least, hide it in plain sight. Python is for "consenting adults",
and private attributes and variables are private by agreement, not
because the language enforces the rule.
(There are a few places in Python where the language does enforce data
hiding, mostly to do with functions, and anything which could cause a
segmentation fault. But regular Python code, not so much. The worst that
happens is you get a regular Python exception.)
Interesting that you refer to "OO people". There's no widespread
agreement about what makes an Object Oriented Programming language
(although Java people tend to think that Java offers the One True OOP
language *wink*) but I think the most critical requirement is for the
language to offer "objects" as a native data type, where objects are
structured entities which combine:
- behaviour (methods)
- state (data, value)
- and identity (unique existence).
The first two make up encapsulation, or at least a form of
encapsulation. Data hiding itself is not a requirement, and even
inheritence isn't. (Although it is unusual to have OOP without a form of
inheritence.)
--
Steve
More information about the Tutor
mailing list