Private attribute
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Mon Aug 25 19:55:04 EDT 2008
On Mon, 25 Aug 2008 19:47:38 +0100, Ken Starks wrote:
> I have a class with an attribute called 'gridsize' and I want a derived
> class to force and keep it at 0.8 (representing 8mm).
>
> Is this a correct, or the most pythonic approach?
Others have already suggested using a property, but I'll suggest that the
most Pythonic approach is not to be so dogmatic about trying to force it
to stay at 0.8. Maybe *you* will never want it to change, but can you be
sure that those calling your class will never want a 9mm grid?
I'd suggest marking it private by convention:
def SomeClass(object):
_gridsize = 0.8
The leading underscore tells callers that they change the attribute at
their own risk.
An even more Pythonic approach is to write your class that makes no
assumptions about gridsize, and thus explicitly supports any reasonable
grid size.
--
Steven
More information about the Python-list
mailing list