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


Groups > comp.lang.python > #32404

Re: Nice solution wanted: Hide internal interfaces

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!rt.uk.eu.org!feed.xsnews.nl!border-1.ams.xsnews.nl!xlned.com!feeder7.xlned.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
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; 'ugly': 0.07; 'instance.': 0.09; 'name)': 0.09; 'name):': 0.09; 'objects.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.10; '(but': 0.15; '"private': 0.16; 'a()': 0.16; 'a(object):': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'subject:interfaces': 0.16; 'wrote:': 0.17; 'instance': 0.17; '>>>': 0.18; 'this:': 0.23; 'header:User-Agent:1': 0.26; 'c++': 0.27; 'header:X-Complaints-To:1': 0.28; 'dictionary': 0.29; 'methods.': 0.29; 'obj': 0.29; 'wrap': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; "i'm": 0.29; 'maybe': 0.29; 'function': 0.30; 'print': 0.32; 'to:addr:python-list': 0.33; "can't": 0.34; 'received:org': 0.36; 'but': 0.36; '(i.e.': 0.36; 'does': 0.37; 'subject:: ': 0.38; 'skip:o 20': 0.38; 'some': 0.38; 'sure': 0.38; 'there,': 0.38; 'gives': 0.39; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'think': 0.40; 'easy': 0.60; 'friend': 0.81; 'subject:Nice': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Nice solution wanted: Hide internal interfaces
Date Mon, 29 Oct 2012 18:06:30 +0100
Organization None
References <k6mb4k$5v3$1@news.albasani.net>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p50849008.dip.t-dialin.net
User-Agent KNode/4.7.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3032.1351530395.27098.python-list@python.org> (permalink)
Lines 35
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1351530395 news.xs4all.nl 6951 [2001:888:2000:d::a6]:53366
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:32404

Show key headers only | View raw


Johannes Bauer wrote:

> 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.
> 
> Currently my ugly approach is this: I delare the internal methods
> private (hide from user). Then I have a function which gives me a
> dictionary of callbacks to the private functions of the other objects.
> This is in my opinion pretty ugly (but it works and does what I want).
> 
> I'm pretty damn sure there's a nicer (prettier) solution out there, but
> I can't currently think of it. Do you have any hints?

Maybe you  can wrap A into a class that delegates to A:

>>> class A(object):
...     def __private(self): print "private A"
... 
>>> class Friend(object):
...     def __init__(self, obj):
...             self.__obj = obj
...             self.__prefix = "_%s_" % obj.__class__.__name__
...     def __getattr__(self, name):
...             return getattr(self.__obj, self.__prefix + name)
... 
>>> a = A()
>>> a._A__private() # hard
private A
>>> f = Friend(a)
>>> f._private() # easy
private A

The B instance would refer to A via the Friend instance.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Nice solution wanted: Hide internal interfaces Johannes Bauer <dfnsonfsduifb@gmx.de> - 2012-10-29 17:33 +0100
  Re: Nice solution wanted: Hide internal interfaces andrea crotti <andrea.crotti.0@gmail.com> - 2012-10-29 16:41 +0000
  Re: Nice solution wanted: Hide internal interfaces Benjamin Kaplan <benjamin.kaplan@case.edu> - 2012-10-29 09:41 -0700
  Re: Nice solution wanted: Hide internal interfaces Chris Angelico <rosuav@gmail.com> - 2012-10-30 03:47 +1100
    Re: Nice solution wanted: Hide internal interfaces Johannes Bauer <dfnsonfsduifb@gmx.de> - 2012-10-29 17:58 +0100
      Re: Nice solution wanted: Hide internal interfaces Paul Rubin <no.email@nospam.invalid> - 2012-10-29 10:03 -0700
      Re: Nice solution wanted: Hide internal interfaces Grant Edwards <invalid@invalid.invalid> - 2012-10-29 18:00 +0000
      Re: Nice solution wanted: Hide internal interfaces Ian Kelly <ian.g.kelly@gmail.com> - 2012-10-29 13:09 -0600
  Re: Nice solution wanted: Hide internal interfaces Grant Edwards <invalid@invalid.invalid> - 2012-10-29 16:52 +0000
    Re: Nice solution wanted: Hide internal interfaces Johannes Bauer <dfnsonfsduifb@gmx.de> - 2012-10-29 18:01 +0100
      Re: Nice solution wanted: Hide internal interfaces Peter Otten <__peter__@web.de> - 2012-10-29 18:17 +0100
  Re: Nice solution wanted: Hide internal interfaces Peter Otten <__peter__@web.de> - 2012-10-29 18:06 +0100
  Re: Nice solution wanted: Hide internal interfaces Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-29 22:37 +0000
  Re: Nice solution wanted: Hide internal interfaces alex23 <wuwei23@gmail.com> - 2012-10-29 18:37 -0700
    Re: Nice solution wanted: Hide internal interfaces andrea crotti <andrea.crotti.0@gmail.com> - 2012-10-30 14:15 +0000

csiph-web