Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed5.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'received:209.85.223': 0.03; 'explicitly': 0.04; 'context': 0.05; 'problem:': 0.07; 'wrapper': 0.07; 'api': 0.09; 'commonly': 0.09; 'methods,': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; '"from': 0.16; '__init__.py': 0.16; 'b()': 0.16; 'oct': 0.16; 'partly': 0.16; 'subject:interfaces': 0.16; 'wrote:': 0.17; 'define': 0.20; 'import': 0.21; 'cc:2**0': 0.23; 'user.': 0.23; 'cc:no real name:2**0': 0.24; 'pass': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; '(which': 0.26; 'am,': 0.27; 'separate': 0.27; 'c++': 0.27; 'message-id:@mail.gmail.com': 0.27; 'interface': 0.27; "doesn't": 0.28; 'methods.': 0.29; 'url:mailman': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; "i'm": 0.29; 'classes': 0.30; 'url:python': 0.32; 'file': 0.32; 'could': 0.32; 'url:listinfo': 0.32; 'skip:s 30': 0.33; 'instances': 0.33; 'received:google.com': 0.34; 'done': 0.34; 'clear': 0.35; 'doing': 0.35; 'received:209.85': 0.35; 'created': 0.36; 'but': 0.36; 'url:org': 0.36; '(i.e.': 0.36; 'method': 0.36; 'useful': 0.36; 'skip:p 20': 0.36; 'does': 0.37; 'two': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'header:Received:5': 0.40; 'url:mail': 0.40; 'think': 0.40; 'your': 0.60; '30,': 0.62; 'show': 0.63; 'choose': 0.65; '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 :cc:content-type; bh=wD1o6u2o3TkO6S80NRmtxImDXCgrqulQfvYyRIHeQcQ=; b=k/XPH9S0hBD+Lvc+sExadvUn98SElz9QtRGXJUVhxqEMGWp7m1xrk0Dcv3+zjPrMQn d0Tzces8OyyulUTnQPwbOZdDI3uhcYhQjgfNeEcjxAVj49J/HkI0Lh7qc47KrKu3dGSB zWfX1jZRai8Cp8k8Vm11S+1y7lcdRz25HHg8eh0oXbqMM/4pmdY+EMN52p6eQQERFwNR VmaslBuBbs6afn2fqNUlc9VIZ8oqWHvbJLNJZ0DIfPFvJqZ3pwxqZTywPnuBGc32p8Dd Vh6M/KxAOX1tAKtz3o5aTRgaQeLX5iPZgt1RCmZfYxJRAMktkCwd1643FZctEQpCCZrV TKtw== MIME-Version: 1.0 In-Reply-To: <761032ae-6c92-4b80-b287-e158ece7582e@kt16g2000pbb.googlegroups.com> References: <761032ae-6c92-4b80-b287-e158ece7582e@kt16g2000pbb.googlegroups.com> Date: Tue, 30 Oct 2012 14:15:09 +0000 Subject: Re: Nice solution wanted: Hide internal interfaces From: andrea crotti To: alex23 Content-Type: text/plain; charset=ISO-8859-1 Cc: python-list@python.org 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: 67 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1351606518 news.xs4all.nl 6876 [2001:888:2000:d::a6]:54372 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32492 2012/10/30 alex23 : > On Oct 30, 2:33 am, Johannes Bauer wrote: >> 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. > > One approach could be to only have the public interface on B, and then > create a wrapper for B that provides the private interface: > > class B: > def public_method(self): > pass > > class B_Private: > def __init__(self, context): > self.context = context > > def private_method(self): > # manipulate self.context > > class A: > def __init__(self): > self.b = B() > self.b_private = B_Private(self.b) > > def foo(self): > # call public method > self.b.public_method() > > # call private method > self.b_private.private_method() > > It doesn't stop a user from accessing the private methods, but it does > separate them so they have to *intentionally* choose to use them. > -- > http://mail.python.org/mailman/listinfo/python-list Partly unrelated, but you could also define a clear API and expose it through your __init__.py. For example: package/a.py: class A: pass package/b.py: class B:pass package/__init__.py from a import A so now doing "from package import" will only show A. This doesn't work on the method-level, but it's useful to know and commonly done in many projects.. In some projects they even use a file "api.py" to you have to explicitly import from package.api import .. (which I think is overkill since __init__.py does the same)