Not possible to hide local variables
Cecil Westerhof
Cecil at decebal.nl
Wed Apr 29 02:16:01 EDT 2015
Op Tuesday 28 Apr 2015 09:56 CEST schreef Ethan Furman:
> On 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?
>
> Prefix those names with a single leading underscore, which is the
> convention for private variables.
Done.
> This way, if some user (maybe you! ;) has a good reason to change
> those values in can be done, but it is quite clear that said user is
> mucking about with internals and they are on their own.
There is something to say for this way of working.
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
More information about the Python-list
mailing list