Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #89594

Re: Not possible to hide local variables

Date 2015-04-29 22:31 -0400
From Dave Angel <davea@davea.name>
Subject Re: Not possible to hide local variables
References <874mo0zoz2.fsf@Equus.decebal.nl> <mhqp33$dpj$2@reader1.panix.com>
Newsgroups comp.lang.python
Message-ID <mailman.109.1430361094.3680.python-list@python.org> (permalink)

Show all headers | View raw


On 04/29/2015 10:16 AM, Grant Edwards wrote:
> On 2015-04-28, Cecil Westerhof <Cecil@decebal.nl> 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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Not possible to hide local variables Cecil Westerhof <Cecil@decebal.nl> - 2015-04-28 09:33 +0200
  Re: Not possible to hide local variables Ethan Furman <ethan@stoneleaf.us> - 2015-04-28 00:56 -0700
    Re: Not possible to hide local variables Cecil Westerhof <Cecil@decebal.nl> - 2015-04-29 08:16 +0200
      Re: Not possible to hide local variables Michael Torrie <torriem@gmail.com> - 2015-04-29 21:10 -0600
  Re: Not possible to hide local variables Chris Angelico <rosuav@gmail.com> - 2015-04-28 18:06 +1000
    Re: Not possible to hide local variables Cecil Westerhof <Cecil@decebal.nl> - 2015-04-29 08:25 +0200
      Re: Not possible to hide local variables Chris Angelico <rosuav@gmail.com> - 2015-04-29 17:36 +1000
  Re: Not possible to hide local variables Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-04-28 18:37 +1000
    Re: Not possible to hide local variables Chris Angelico <rosuav@gmail.com> - 2015-04-28 18:44 +1000
    Re: Not possible to hide local variables Marko Rauhamaa <marko@pacujo.net> - 2015-04-28 12:20 +0300
    Re: Not possible to hide local variables Cecil Westerhof <Cecil@decebal.nl> - 2015-04-29 08:32 +0200
      Re: Not possible to hide local variables Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-04-30 14:05 +1000
        Re: Not possible to hide local variables Marko Rauhamaa <marko@pacujo.net> - 2015-04-30 08:10 +0300
          Re: Not possible to hide local variables Chris Angelico <rosuav@gmail.com> - 2015-04-30 16:01 +1000
  Re: Not possible to hide local variables Cecil Westerhof <Cecil@decebal.nl> - 2015-04-29 08:14 +0200
  Re: Not possible to hide local variables Grant Edwards <invalid@invalid.invalid> - 2015-04-29 14:16 +0000
    Re: Not possible to hide local variables Dave Angel <davea@davea.name> - 2015-04-29 22:31 -0400
    Re: Not possible to hide local variables Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-04-30 08:53 -0400
      Re: Not possible to hide local variables Cecil Westerhof <Cecil@decebal.nl> - 2015-04-30 16:50 +0200

csiph-web