Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #101546
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Jason Swails <jason.swails@gmail.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: What use of these _ prefix members? |
| Date | Tue, 12 Jan 2016 10:01:57 -0500 |
| Lines | 55 |
| Message-ID | <mailman.64.1452610921.13488.python-list@python.org> (permalink) |
| References | <4ee5810e-abe9-4c4b-9ebd-d989b5c2b341@googlegroups.com> <mailman.7.1452450908.3151.python-list@python.org> <n731jm$1apr$1@gioia.aioe.org> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| Content-Transfer-Encoding | quoted-printable |
| X-Trace | news.uni-berlin.de Tdu1l6mTU1wOQKOL+IbltgJ2ndcBeKZZkdmfu0SJOZ2Q== |
| Return-Path | <jason.swails@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; 'sufficient': 0.05; 'override': 0.07; 'cc:addr:python-list': 0.09; '*args': 0.09; '__init__': 0.09; 'derived': 0.09; 'subclasses': 0.09; 'subject:members': 0.09; 'units.': 0.09; 'example:': 0.10; 'jan': 0.11; 'def': 0.13; 'instead.': 0.15; '*this*': 0.16; '2016': 0.16; '__init__,': 0.16; 'cc:name:python list': 0.16; 'different,': 0.16; 'inheritance': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'sfxlen:0': 0.16; 'wrote:': 0.16; 'example.': 0.18; '>': 0.18; '(not': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'class,': 0.22; 'object.': 0.22; 'suppose': 0.22; 'pass': 0.22; 'am,': 0.23; 'split': 0.23; 'header:In-Reply-To:1': 0.24; "i've": 0.25; "doesn't": 0.26; 'example': 0.26; 'sense': 0.26; 'message- id:@mail.gmail.com': 0.27; 'directly,': 0.29; 'improves': 0.29; 'sure,': 0.29; 'allows': 0.30; 'code': 0.30; 'class.': 0.30; 'skip:s 30': 0.31; 'skip:_ 10': 0.32; 'implement': 0.32; 'class': 0.33; 'tue,': 0.34; 'skip:& 20': 0.35; 'received:google.com': 0.35; 'best,': 0.35; 'clear': 0.35; 'easiest': 0.35; 'jason': 0.35; 'subject:use': 0.35; 'something': 0.35; 'sometimes': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'skip:& 10': 0.37; '12,': 0.37; 'received:209.85.213': 0.37; 'doing': 0.38; 'received:209': 0.38; 'anything': 0.38; 'enough': 0.39; 'called': 0.40; 'more': 0.63; 'necessarily': 0.63; 'times': 0.63; 'benefit': 0.66; 'numerous': 0.66; 'skip:\xc2 10': 0.67; '8bit%:40': 0.72; 'construction': 0.72; 'arg1,': 0.84; "class's": 0.84; 'duplication': 0.84; 'justified.': 0.84; 'otten': 0.84; 'x):': 0.84; '8bit%:18': 0.93 |
| 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=xI8paF/SKFAD4fdV2v40pXYmnWiiPHPugACPfKygVao=; b=QZ5sBLhqbCLVcXiSwE8vFA/xUtlOPSOVG3sZZ00A+3wJZb40JIwsaWDeuywM19TnRY P2nqDA3A5Ij724AEW2m5wAy6aVTvDb1wvL91V+CS/156j4YP4oDEcraoGba22enq+6x3 gCLUBjnjrCHB/q1Y3WUY5H63d2Y1lTUGwRSD1e3NPhLVFpYTMh6oPwoLXrFgaOr3iZo1 lYRViah0PzqX6XVF3629ZfqKNL6qjQbfvoo9RAk4m9jxKdt7ePYpSHH2K7ChbG2hEPP0 8lQboy7z9hJz/MPygkAv9NtQYXSLxIg+YlkTQYyz8mIHVEICvKt42CDe3UomvZJZeomT UHpA== |
| X-Received | by 10.31.157.204 with SMTP id g195mr56219548vke.79.1452610917915; Tue, 12 Jan 2016 07:01:57 -0800 (PST) |
| In-Reply-To | <n731jm$1apr$1@gioia.aioe.org> |
| X-Content-Filtered-By | Mailman/MimeDel 2.1.20+ |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.20+ |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://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 | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Xref | csiph.com comp.lang.python:101546 |
Show key headers only | View raw
On Tue, Jan 12, 2016 at 9:12 AM, me <self@example.org> wrote:
> On 2016-01-10, Peter Otten <__peter__@web.de> wrote:
> >>>> class Derived(Base):
> > ... def _init(self, x):
> > ... super()._init(x)
> > ... print("do something else with", x)
> > ...
> >>>> Derived(42)
> > do something with 42
> > do something else with 42
> ><__main__.Derived object at 0x7f8e6b3e9b70>
> >
>
> I think you are doing inheritance wrong.
>
There's nothing "wrong" about this, and there are times this type of
pattern is justified. Sure, *this* example doesn't make sense to do it
this way, but this is just an illustrative example. I would even call this
type of pattern pythonic.
> AFAIK you should call directly the __init__() of the parent class, and
>
> pass *args and **kwargs instead.
>
Sometimes there's no need to call __init__ on the parent class directly,
and the base class's __init__ is sufficient for the derived class. And
perhaps initialization requires numerous "steps" that are easiest to grok
when split out into different, private sub-methods. For example:
class Derived(Base):
def __init__(self, arg1, arg2, arg3):
self._initial_object_setup()
self._process_arg1(arg1)
self._process_arg23(arg2, arg3)
self._postprocess_new_object()
This makes it clear what is involved in the initialization of the new
object. And it allows the functionality to be split up into more atomic
units. It also has the added benefit of subclasses being able to more
selectively override base class functionality. Suppose Derived only needs
to change how it reacts to arg1 -- all Derived needs to implement directly
is _process_arg1. This reduces code duplication and improves
maintainability, and is a pattern I've used myself and like enough to use
again (not necessarily in __init__, but outside of being automatically
called during construction I don't see anything else inherently "specialer"
about __init__ than any other method).
All the best,
Jason
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
What use of these _ prefix members? Robert <rxjwg98@gmail.com> - 2016-01-10 09:55 -0800
Re: What use of these _ prefix members? Steven D'Aprano <steve@pearwood.info> - 2016-01-11 05:03 +1100
Re: What use of these _ prefix members? Peter Otten <__peter__@web.de> - 2016-01-10 19:34 +0100
Re: What use of these _ prefix members? me <self@example.org> - 2016-01-12 14:12 +0000
Re: What use of these _ prefix members? Peter Otten <__peter__@web.de> - 2016-01-12 15:52 +0100
Re: What use of these _ prefix members? Jason Swails <jason.swails@gmail.com> - 2016-01-12 10:01 -0500
csiph-web