Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!nntp.club.cc.cmu.edu!micro-heart-of-gold.mit.edu!bloom-beacon.mit.edu!bloom-beacon.mit.edu!panix!not-for-mail From: Grant Edwards Newsgroups: comp.lang.python Subject: Re: Not possible to hide local variables Date: Wed, 29 Apr 2015 14:16:03 +0000 (UTC) Organization: PANIX Public Access Internet and UNIX, NYC Lines: 40 Message-ID: References: <874mo0zoz2.fsf@Equus.decebal.nl> NNTP-Posting-Host: 67-130-15-94.dia.static.qwest.net X-Trace: reader1.panix.com 1430316963 14131 67.130.15.94 (29 Apr 2015 14:16:03 GMT) X-Complaints-To: abuse@panix.com NNTP-Posting-Date: Wed, 29 Apr 2015 14:16:03 +0000 (UTC) User-Agent: slrn/1.0.1 (Linux) Xref: csiph.com comp.lang.python:89552 On 2015-04-28, Cecil Westerhof wrote: > If I remember correctly you can not hide variables of a class or make > them read-only? > > I want to rewrite my moving average to python. The init is: > def __init__(self, length): > if type(length) != int: > raise ParameterError, 'Parameter has to be an int' > if n < 0: > raise ValueError, 'Parameter should be greater or equal 2' > self.length = length > self.old_values = [] > self.current_total = 0 > > But when someone changes length, old_values, or current_total that > would wreck havoc with my class instance. What is the best way to > handle this? It's like the punchline to the old doctor joke: if it hurts when you do that, then don't _do_ that: def __init__(self, length): if type(length) != int: raise ParameterError, 'Parameter has to be an int' if n < 0: raise ValueError, 'Parameter should be greater or equal 2' self._length = length self._old_values = [] self._current_total = 0 The convention is that properties that start with underscores are private. They're not hidden, but if people touch them and it breaks something, it's their fault. Whether you go all Torvalds on their ass for doing so is left as an exercise for the reader. -- Grant Edwards grant.b.edwards Yow! If Robert Di Niro at assassinates Walter Slezak, gmail.com will Jodie Foster marry Bonzo??