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


Groups > comp.lang.python > #101545

Re: What use of these _ prefix members?

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: What use of these _ prefix members?
Date Tue, 12 Jan 2016 15:52:18 +0100
Organization None
Lines 50
Message-ID <mailman.63.1452610366.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="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Trace news.uni-berlin.de lBcnN8S0Cnl0UJklLMqtbA5RIOSzggJTvp62975a8sKw==
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.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'source,': 0.04; '*args,': 0.07; 'override': 0.07; '*args': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subclasses': 0.09; 'subject:members': 0.09; 'def': 0.13; 'instead.': 0.15; 'distinct': 0.16; 'inheritance': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'wrote:': 0.16; 'looked': 0.16; 'arguments': 0.22; 'class,': 0.22; 'own.': 0.22; 'pass': 0.22; 'decide': 0.23; 'header:User-Agent:1': 0.26; 'header:X-Complaints- To:1': 0.26; 'sense': 0.26; 'question': 0.27; 'turns': 0.27; 'function': 0.28; 'actual': 0.28; 'invoke': 0.29; 'code': 0.30; 'option': 0.31; 'posting': 0.32; 'class': 0.33; 'foo': 0.33; 'though.': 0.33; 'except': 0.34; 'that,': 0.34; 'generic': 0.35; 'robert': 0.35; 'subject:use': 0.35; 'something': 0.35; 'expected': 0.35; 'sometimes': 0.35; 'should': 0.36; 'instead': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'method': 0.37; 'received:org': 0.37; 'doing': 0.38; "won't": 0.38; 'someone': 0.38; 'mean': 0.38; 'rather': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'software': 0.40; 'sample': 0.63; '>>>>>': 0.66; 'elsewhere': 0.66; 'endorsement.': 0.84; 'enforced': 0.84; 'otten': 0.84; 'private.': 0.84; 'x):': 0.84; 'approach.': 0.91
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host p57bd8ad4.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
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:101545

Show key headers only | View raw


Someone else posting as "me" 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.

If by "you" you mean "me" -- the above sample is an illustration of the 
pattern I expected to see elsewhere in the software Robert was quoting, not 
an endorsement. I have now looked into the hmmlearn source, and it turns out 
that _init() is invoked by the fit() method rather than the initializer. 
But...
 
> AFAIK you should call directly the __init__() of the parent class, and
> pass *args and **kwargs instead.

you sometimes want to break initialisation or any other method into distinct 
steps that don't make sense stand-alone:

class Foo:
    def method(...)
        self._one(...)
        self._two(...)
        self._three(...)

That way subclasses have the option to override only _two() instead of 
method() and users of Foo won't try to invoke _two(...) on its own.

I think this is a good approach.

What arguments you need to accept and/or pass on is a question that you can 
decide depending on the actual use-case. I use

*args, **kwargs

only if function is very generic because it makes code hard to follow.

> Except for that, yes, the _init would be conventionally private. Not
> enforced by name mangling though.

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


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