Path: csiph.com!usenet.pasdenom.info!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'exercise': 0.04; 'init': 0.07; 'variables': 0.07; 'instance.': 0.09; 'rewrite': 0.09; 'subject:hide': 0.09; 'def': 0.12; 'changes': 0.15; '_do_': 0.16; 'ass': 0.16; 'better:': 0.16; 'hidden,': 0.16; 'hurts': 0.16; 'int):': 0.16; 'length,': 0.16; 'subject:Not': 0.16; 'subject:possible': 0.16; 'undefined)': 0.16; 'underscores': 0.16; 'valueerror,': 0.16; 'wrote:': 0.18; 'this?': 0.23; 'header:User-Agent:1': 0.23; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'properties': 0.29; 'raise': 0.29; '(since': 0.31; 'class': 0.32; 'skip:_ 10': 0.34; 'equal': 0.35; 'but': 0.35; 'doing': 0.36; 'should': 0.36; 'convention': 0.38; 'handle': 0.38; 'to:addr:python-list': 0.38; 'that,': 0.38; 'moving': 0.39; 'to:addr:python.org': 0.39; 'length': 0.61; 'charset:windows-1252': 0.65; 'received:74.208': 0.68; 'touch': 0.74; 'private.': 0.84; 'doctor': 0.91; 'edwards': 0.91; 'average': 0.93 Date: Wed, 29 Apr 2015 22:31:13 -0400 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Not possible to hide local variables References: <874mo0zoz2.fsf@Equus.decebal.nl> In-Reply-To: Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V03:K0:B7pofN6exu9p88z59VknIUAtNRqSm498mH5InyXTMV3gnKrVG8u dF8Cy8GBuNGJpcRwcXbbP6m1n0K1yabRTH7ZQUd+i13eAxRXcxkVmJlzK/dymRZzNTJhJl9 BtZeXFaHCJBvZqou7I52SkUmhM9jj8TakQwR0qRtIc9uicJRwaqNHHVXHvI3cmx6Q1ocmya JR9ryY3We9tyLLdWkx1RQ== X-UI-Out-Filterresults: notjunk:1; X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 46 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1430361094 news.xs4all.nl 2899 [2001:888:2000:d::a6]:47892 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:89594 On 04/29/2015 10:16 AM, Grant Edwards wrote: > 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: Better: if isinstance(length, int): > raise ParameterError, 'Parameter has to be an int' > if n < 0: Better: if length < 0: (since n is undefined) > 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. > -- DaveA