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


Groups > comp.lang.python > #89522

Re: Not possible to hide local variables

From Cecil Westerhof <Cecil@decebal.nl>
Newsgroups comp.lang.python
Subject Re: Not possible to hide local variables
Organization Decebal Computing
References <874mo0zoz2.fsf@Equus.decebal.nl> <553f46d2$0$11124$c3e8da3@news.astraweb.com>
Date 2015-04-29 08:32 +0200
Message-ID <87h9rzxx5h.fsf@Equus.decebal.nl> (permalink)

Show all headers | View raw


Op Tuesday 28 Apr 2015 10:37 CEST schreef Steven D'Aprano:

> On Tuesday 28 April 2015 17:33, Cecil Westerhof wrote:
>
>> If I remember correctly you can not hide variables of a class or
>> make them read-only?
>
> In Python circles, the preferred terminology for class and instance
> members is "attributes" rather than variables. "Variable" is
> reserved for module- and function-level name bindings.

I will try to remember that.


> But other than that, you are correct. Python practices attribute
> hiding by convention. Names beginning with a single underscore like
> _spam or obj._eggs are treated as private.

Using that now.


> Names beginning with two underscores, but not trailing with
> underscores, are also name-mangled: obj.__eggs will be mangled to
> obj._TheClass__eggs. You should avoid name-mangling unless you
> really need it, don't use it "just in case".

What is a good reason?


> The convention is, if the caller messes with your private attributes
> or variables, and their code breaks, they have nobody to blame but
> themselves, and we are allowed to laugh at them. We're consenting
> adults here.

Coming from C/C++ and Java I have to get used to it, but I will manage
I think.


> Don't use a class at all. Moving average is best handled as a
> generator. We can use a regular generator:

Not in this case. The idea is that you do not know the elements at the
moment, but feed them to the MovingAverage instance as they come in.
See:
    https://github.com/CecilWesterhof/PythonLibrary/blob/master/utilDecebal.py


> If you must use a class, flag the internal attributes as private
> with a leading underscore. Your callers will respect that, and if
> they don't, all promises are null and void.

Done.

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

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