Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!news1.tnib.de!feed.news.tnib.de!news.tnib.de!newsfeed.freenet.ag!news2.euro.net!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'none:': 0.05; 'class,': 0.07; 'nested': 0.07; 'problem:': 0.07; 'convention,': 0.09; 'underscore': 0.09; 'def': 0.10; '"this': 0.13; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'internal:': 0.16; 'oct': 0.16; 'subject:interfaces': 0.16; 'world!")': 0.16; 'wrote:': 0.17; 'received:209.85.214.174': 0.21; 'sends': 0.22; 'skip:_ 20': 0.22; 'dependent': 0.23; 'this:': 0.23; 'user.': 0.23; 'header:In-Reply-To:1': 0.25; 'am,': 0.27; 'c++': 0.27; 'convention': 0.27; 'message-id:@mail.gmail.com': 0.27; "doesn't": 0.28; 'methods.': 0.29; 'definition': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; "i'm": 0.29; 'classes': 0.30; 'instances': 0.33; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'stable': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'created': 0.36; 'but': 0.36; '(i.e.': 0.36; 'two': 0.37; 'usual': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'there,': 0.38; 'to:addr:python.org': 0.39; 'received:209.85.214': 0.39; 'header:Received:5': 0.40; 'leading': 0.61; '30,': 0.62; 'promise': 0.65; 'friends': 0.83; 'subject:Nice': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=jqIaD8tPSmYXj9n39sIIj9ICZ9NvtrPmpEswA20Ye98=; b=VGLDkIRK9Wnfk6dR5xImwHDs1PdZ8rvwwiephQEt5LHjKXZ5x4MdBEXvpMHtEndaab cCAKFIhjxagypyoiFtprK7YhIBMRmp4JFdBUuNRwducMLjtt8Kr3KxmNh3fmCIcjihaX bgNANOyEilCFsgFmaL3BszFwW42vAnYm1n74oBvMKFmaHBRM1YiSB6aJDqT5gkd2tEm/ P8adGGxxr9QZFGxdIO7Oz3C+nIiC9dO2q1ZvIpxUNp3IL+SSzpKk3+Np9P7upxfPHo9L +lJUDUI3EZKhMIV+vpEZtOSlb9r8aYCJcUoN5J8cyj9PXAGWHoY9aOlinfaxFI+6oyyP Ko7Q== MIME-Version: 1.0 In-Reply-To: References: Date: Tue, 30 Oct 2012 03:47:21 +1100 Subject: Re: Nice solution wanted: Hide internal interfaces From: Chris Angelico To: python-list@python.org 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: 41 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1351529244 news.xs4all.nl 6924 [2001:888:2000:d::a6]:58318 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32398 On Tue, Oct 30, 2012 at 3:33 AM, Johannes Bauer wrote: > Hi there, > > I'm currently looking for a good solution to the following problem: I > have two classes A and B, which interact with each other and which > interact with the user. Instances of B are always created by A. > > Now I want A to call some private methods of B and vice versa (i.e. what > C++ "friends" are), but I want to make it hard for the user to call > these private methods. The usual convention for private methods is a leading underscore on the name: class A: def foo(self): print("Fooing!") def _bar(self): print("Only my friends may bar me.") It's only a convention, though; it doesn't make it "hard" to call them, it just sends the message "this is private, I don't promise that it'll be stable across versions". Incidentally, you may want to use a nested class, if the definition of B is entirely dependent on A. Something like this: class A: class B: def _asdf(self,newval=None): if newval is not None: self._val=newval return self._val def _qwer(self,parent): parent._bar("My value is: "+self._val) def foo(self): self.b=self.B() self.b._asdf("Hello, world!") self.b._qwer(self) def _bar(self,msg): print("Message from internal: "+msg) ChrisA