Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'argument': 0.05; 'say,': 0.05; 'subject:Python': 0.06; 'attribute': 0.07; 'correct.': 0.07; 'level,': 0.07; 'parser': 0.07; 'convention,': 0.09; 'creation,': 0.09; 'part,': 0.09; 'referenced': 0.09; 'warn': 0.09; 'python': 0.11; 'def': 0.12; 'changes': 0.15; 'bases,': 0.16; 'behavior,': 0.16; 'benefit.': 0.16; 'charles': 0.16; 'fits': 0.16; 'idle,': 0.16; 'interpreter,': 0.16; 'invokes': 0.16; 'itself,': 0.16; 'marco': 0.16; 'message-id:@earthlink.net': 0.16; 'metaclass': 0.16; 'name)': 0.16; 'otoh,': 0.16; 'parser.': 0.16; 'received:dsl.mindspring.com': 0.16; 'sees': 0.16; 'subject:class': 0.16; 'exception': 0.16; 'so.': 0.16; 'language': 0.16; 'wrote:': 0.18; 'normally': 0.19; '>>>': 0.22; '(in': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'section.': 0.24; 'fine': 0.24; 'question': 0.24; "i've": 0.25; 'header:In-Reply- To:1': 0.27; 'external': 0.29; '(this': 0.29; 'am,': 0.29; 'mode': 0.30; "i'm": 0.30; 'included': 0.31; "skip:' 10": 0.31; "d'aprano": 0.31; 'idea,': 0.31; 'occurs': 0.31; 'option.': 0.31; 'steven': 0.31; 'class': 0.32; 'run': 0.32; 'another': 0.32; 'quite': 0.32; 'checking': 0.33; 'totally': 0.33; 'skip:_ 10': 0.34; 'subject:the': 0.34; 'could': 0.34; 'agree': 0.35; 'except': 0.35; 'tool': 0.35; 'something': 0.35; 'no,': 0.35; 'but': 0.35; 'add': 0.35; 'there': 0.35; 'really': 0.36; '+0200,': 0.36; 'raising': 0.36; 'done': 0.36; 'possible': 0.36; 'should': 0.36; 'clear': 0.37; 'convention': 0.38; 'version,': 0.38; 'to:addr :python-list': 0.38; 'rather': 0.38; 'does': 0.39; 'bad': 0.39; 'itself': 0.39; 'to:addr:python.org': 0.39; 'catch': 0.60; 'is.': 0.60; 'up,': 0.60; 'full': 0.61; 'mentioned': 0.61; 'show': 0.63; 'name': 0.63; 'real': 0.63; 'happen': 0.63; 'skip:n 10': 0.64; 'more': 0.64; 'different': 0.65; 'kept': 0.65; 'inform': 0.78; '100': 0.79; "'foo'": 0.84; 'answer:': 0.84; '2013': 0.98 Date: Wed, 09 Oct 2013 12:34:46 -0700 From: Charles Hixson User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130922 Icedove/17.0.9 MIME-Version: 1.0 To: python-list@python.org Subject: Re: class-private names and the Zen of Python References: <52540779$0$29984$c3e8da3$5496439d@news.astraweb.com> In-Reply-To: <52540779$0$29984$c3e8da3$5496439d@news.astraweb.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 72 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1381347542 news.xs4all.nl 15935 [2001:888:2000:d::a6]:60475 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:56513 On 10/08/2013 06:24 AM, Steven D'Aprano wrote: > On Tue, 08 Oct 2013 12:13:48 +0200, Marco Buttu wrote: > >> In the following case: >> >> >>> class Foo: >> ... _Foo__a = 100 >> ... __a = 33 >> ... >> >>> Foo._Foo__a >> 33 >> >> I think this behavior, for a user who does not know the convention, >> could be a surprise. > Yes, you are correct. It surprised me, and I've been using Python for > more than 15 years, and I know the convention of double-underscore name- > mangling. > > >> Should be raising an exception (in order to inform >> the user the transformation of the name __a have been replaced an >> existing name) a possible --explicit-- alternative? > No, I don't think so. That would slow down class creation, for no real > benefit. Except for the name-mangling part, this is no different from: > > class Spam: > x = 23 > x = 42 > > If anything, something like PyLint or PyChecker could warn about it. But > the language itself is fine like it is. > > >> Another question is: where is the place in which this transformation >> occurs? Is it at the parser level, before the dictionary attribute is >> gave as argument to the metaclass? > Good question! > > I don't have a full answer, but I have a part answer: it occurs before > the metaclass sees the namespace: > > py> class Meta(type): > ... def __new__(meta, name, bases, namespace): > ... print(namespace) > ... return super().__new__(meta, name, bases, namespace) > ... > py> > py> class Test(metaclass=Meta): > ... __test = 'foo' > ... > {'__module__': '__main__', '_Test__test': 'foo', '__qualname__': 'Test'} > > > so I think it is done by the parser. Unless one wanted to add an error checking mode to the interpreter, not a totally bad idea, I agree that this is a job for external tools. But perhaps that same external tool should be referenced in the documentation. As it is I'm not clear that PyLint and/or PyChecker are kept current with the compiler/interpreter version, and I rather suspect that they aren't. (This won't normally show up, as changes that they would catch happen quite rarely, but...) OTOH, neither one really fits in as, say, an included module...they're more like idle, which now that I look does have a "check module" run option. Perhaps that invokes one of them. I note that Idle, itself, is barely mentioned in the documentation. Perhaps there needs to be a "tools" section. -- Charles Hixson