Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #32404
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Subject | Re: Nice solution wanted: Hide internal interfaces |
| Date | 2012-10-29 18:06 +0100 |
| Organization | None |
| References | <k6mb4k$5v3$1@news.albasani.net> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3032.1351530395.27098.python-list@python.org> (permalink) |
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 | Next — Previous in thread | Next in thread | Find similar | Unroll 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