Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #101545
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: What use of these _ prefix members? |
| Date | 2016-01-12 15:52 +0100 |
| Organization | None |
| 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> |
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 | Next — Previous in thread | Next 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