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


Groups > comp.lang.python > #32398

Re: Nice solution wanted: Hide internal interfaces

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 <rosuav@gmail.com>
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 <k6mb4k$5v3$1@news.albasani.net>
References <k6mb4k$5v3$1@news.albasani.net>
Date Tue, 30 Oct 2012 03:47:21 +1100
Subject Re: Nice solution wanted: Hide internal interfaces
From Chris Angelico <rosuav@gmail.com>
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 <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.3030.1351529244.27098.python-list@python.org> (permalink)
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

Show key headers only | View raw


On Tue, Oct 30, 2012 at 3:33 AM, Johannes Bauer <dfnsonfsduifb@gmx.de> 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

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