Path: csiph.com!usenet.pasdenom.info!news.albasani.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.013 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'attribute': 0.05; 'suppose': 0.07; 'def': 0.10; 'itself.': 0.11; 'belonging': 0.16; 'dictionary,': 0.16; 'hashable,': 0.16; 'inheritance': 0.16; 'mangled': 0.16; 'oct': 0.16; 'really?': 0.16; 'wrote:': 0.17; 'tend': 0.17; 'latter': 0.22; "python's": 0.23; 'thus': 0.24; 'header:In-Reply-To:1': 0.25; 'object,': 0.27; 'structures': 0.27; 'message-id:@mail.gmail.com': 0.27; "d'aprano": 0.29; 'relies': 0.29; 'steven': 0.29; 'types.': 0.29; 'unlikely': 0.29; 'objects': 0.29; 'class': 0.29; 'classes': 0.30; 'ends': 0.30; 'waste': 0.30; 'avoiding': 0.33; 'curious': 0.33; 'instances': 0.33; 'problem': 0.33; 'to:addr:python-list': 0.33; 'another': 0.33; "can't": 0.34; 'received:google.com': 0.34; 'whatever': 0.35; 'built-in': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'there': 0.35; 'but': 0.36; 'useful': 0.36; 'does': 0.37; 'being': 0.37; 'passed': 0.37; 'received:209': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'store': 0.38; 'object': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'skip:" 10': 0.40; 'header:Received:5': 0.40; 'time,': 0.62; 'skip:n 10': 0.63; 'more': 0.63; 'avoid.': 0.84; 'to:name:python': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=bA6upNAHpP7GNqlOSW+F8SGgRgdApJ0Dm2IuMTddp0A=; b=sQB5jk7M81o/bK6eK1midW5b6CDyONp6Wdbuhe57odtzkE32IFLmaz0vvKPzZVjEvS Pv2J54ADe9aEL2ffLd1BdZEz8SaZZrPN42BokNWdEy27W+Kl4I+ij7/RJLAp+pFpOoJU MRhJH177/VwCaKzNS/F+JrI2f6KlwU6IKubW3Dt5t+ytUmZ6Y0Vxw7CZWAUKwp2KUz2D fBHD6W5wCAa2IHWZQtHaF5nUu2euop0bGfvVMwoTO6/CX1f+zD0eu8vZWH0QudwMRfT+ EY+bHGWkFttJxr1igILCjHaCYjH5cqddDSo7N/5UZmnY11Busk3PiBKb4RQNqjWHE7Ge EClA== MIME-Version: 1.0 In-Reply-To: <5074b86b$0$6574$c3e8da3$5496439d@news.astraweb.com> References: <1krpbz9.1wge3j11123k2vN%real-not-anti-spam-address@apple-juice.co.uk> <1krpdak.u0qy9e1a4knspN%real-not-anti-spam-address@apple-juice.co.uk> <50742FD4.8030007@gmail.com> <5074b86b$0$6574$c3e8da3$5496439d@news.astraweb.com> From: Ian Kelly Date: Thu, 1 Nov 2012 16:45:22 -0600 Subject: Re: Private methods To: Python Content-Type: text/plain; charset=ISO-8859-1 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: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1351809960 news.xs4all.nl 6917 [2001:888:2000:d::a6]:56360 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32579 On Tue, Oct 9, 2012 at 5:51 PM, Steven D'Aprano wrote: > On Tue, 09 Oct 2012 11:08:13 -0600, Ian Kelly wrote: > >> I tend to view name mangling as being more for avoiding internal >> attribute collisions in complex inheritance structures than for >> designating names as private. > > Really? I tend to view name mangling as a waste of time, and complex > inheritance structures as something to avoid. Name mangling is also useful for object tagging. Suppose you have object A that is passed object B and needs to track some data concerning object B, but does not need a strong reference to B. One solution is to use a weak-key dictionary, but this relies upon B being hashable, and I find it neater to just store the data on B itself. The problem is that whatever name you use might conflict with an existing attribute belonging to B. class Tagger(object): def tag(self, taggee): taggee.__tag = some_tag_data One of the curious ramifications of Python's name-mangling system is that even though the attribute above is being set on some arbitrary object, the mangling that is applied is in any case that of the class Tagger. Thus the mangled attribute name ends up being "_Tagger__tag", which is unlikely to cause a conflict. There are some disadvantages to tagging. One is that you can't tag objects of built-in types. Another is that you can't tag instances of classes with __slots__. I tend to view the latter as another reason to avoid using __slots__.