[ale] Py[h]hon syntax (sic)
Benjamin Scherrey
scherrey at proteus-tech.com
Fri Jan 28 11:45:46 EST 2005
Michael,
Actually python does have a mechanism, albeit a big odd, for making
data private but it is not standard python practice to use getter/setter
methods. Indeed, its generally poor practice in most languages but I see
a lot of poorly written C++ & Java strewn with it. There is the
occasional need for these accessors in some languages but python has
gone even further and completely eliminated their need by explicit
support for properties. Properties allows your class users to use syntax
just like they were accessing the data members directly but actually
routes the access through methods in the class that validate,
manipulate, or generally enforce the interface to that data. Indeed,
through this method one can define read-only (and write-only!)
properties giving your data further protection. This provides for better
enforcement of encapsulation whereas in most cases where you see
getter/setter methods, you've actually identified a violation of
encapsulation and poor design.
-- Ben Scherrey
Michael Hirsch wrote:
> <snip>
>
>No real comments, just a relevant question to the pythonistas out there.
>I notice in this code that there are accessor methods for all the
>variables. This is technically unnecessary since python doesn't have
>private fields like java or C++. However, perhaps it is good style--in
>Java it would be. So, my question is whether this is the preferred way
>to expose fields, or is it considered okay to just access the fields
>directly?
>
>Michael
>
>
> <snip>
>
>
>
>
More information about the Ale
mailing list