Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Mind your own business, Spock. I'm sick of your halfbreed interference.


devel / comp.lang.python / Re: Why doc call `__init__` as a method rather than function?

SubjectAuthor
o Re: Why doc call `__init__` as a method rather than function?Cameron Simpson

1
Re: Why doc call `__init__` as a method rather than function?

<mailman.323.1695015405.23016.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=24019&group=comp.lang.python#24019

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: cs...@cskk.id.au (Cameron Simpson)
Newsgroups: comp.lang.python
Subject: Re: Why doc call `__init__` as a method rather than function?
Date: Mon, 18 Sep 2023 15:16:42 +1000
Lines: 58
Message-ID: <mailman.323.1695015405.23016.python-list@python.org>
References: <OS3P286MB0740BDD307B9BEB732BD2256A5F6A@OS3P286MB0740.JPNP286.PROD.OUTLOOK.COM>
<ZQfdOvw2DoOsctRx@cskk.homeip.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de VP+es09NItBd3uvwZLhelwmYDl1WmIv3Ledv3OWzGX1Q==
Cancel-Lock: sha1:dBdRXfw/5YFckDWFuVoggzlGgZ4= sha256:ymUalSKOFBXcYLHJYONdI6tvNGFoeOtc/tgNLVD+CaI=
Return-Path: <cameron@cskk.id.au>
X-Original-To: Python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=none reason="no signature";
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.001
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'this:': 0.03; 'argument':
0.04; 'def': 0.04; 'knows': 0.04; 'variable': 0.05; 'class.':
0.07; 'subject:Why': 0.07; 'binding': 0.09; 'subject:doc': 0.09;
'cheers,': 0.11; '>>>>': 0.16; '_are_': 0.16; 'accessed': 0.16;
'basic,': 0.16; 'cameron': 0.16; 'expressions.': 0.16;
'from:addr:cs': 0.16; 'from:addr:cskk.id.au': 0.16;
'from:name:cameron simpson': 0.16; 'function...': 0.16;
'instance': 0.16; 'mentioned,': 0.16; 'message-
id:@cskk.homeip.net': 0.16; 'simpson': 0.16; 'subject:call': 0.16;
'wrote:': 0.16; 'python': 0.16; 'calls': 0.19; 'to:addr:python-
list': 0.20; 'i.e.': 0.22; 'skip:_ 10': 0.22; 'to:name:python-
list@python.org': 0.24; 'seems': 0.26; 'object': 0.26; 'function':
0.27; '>>>': 0.28; 'output': 0.28; 'purpose': 0.28; 'it,': 0.29;
'header:User-Agent:1': 0.30; 'putting': 0.31; 'official': 0.32;
'header:In-Reply-To:1': 0.34; 'book': 0.35; 'functions': 0.36;
"it's": 0.37; 'class': 0.37; "we've": 0.39; 'mentioned': 0.39;
'use': 0.39; 'methods': 0.39; 'double': 0.40; 'both': 0.40; 'try':
0.40; 'reference': 0.60; 'method': 0.61; 'pass': 0.64; 'authors':
0.64; 'definition': 0.64; 'your': 0.64; 'look': 0.65; 'named':
0.65; 'received:userid': 0.66; 'header:Received:6': 0.67;
'subject:method': 0.69; 'front': 0.70; 'you.': 0.71; 'see,': 0.76;
'method,': 0.84; 'tao': 0.84; 'subject:than': 0.91
Mail-Followup-To: "python-list@python.org" <Python-list@python.org>
Content-Disposition: inline
In-Reply-To: <OS3P286MB0740BDD307B9BEB732BD2256A5F6A@OS3P286MB0740.JPNP286.PROD.OUTLOOK.COM>
User-Agent: Mutt/2.2.7 (2022-08-07)
X-tce-id: cskk@bigpond.com
X-tce-ares-id: e{ca98fbaf-0c89-41f3-803e-0ed95c85830a}1
X-tce-spam-action: no action
X-tce-spam-score: 0.0
X-Cm-Analysis: v=2.4 cv=c8BtAzxl c=1 sm=1 tr=0 ts=6507dd3c
a=1HOPfUm/oOLO/i5ZczxL5A==:117 a=1HOPfUm/oOLO/i5ZczxL5A==:17
a=-0lRkJj18ndTWBkf:21 a=IkcTkHD0fZMA:10 a=zNV7Rl7Rt7sA:10 a=69EAbJreAAAA:8
a=S4MpH-DmTzVCVC-EOfUA:9 a=QEXdDO2ut3YA:10 a=dIAlLxOHpepzyNjkwDli:22
X-Cm-Envelope: MS4xfCg/v0/TIXoR1s8j6IaXEMri6IZskvHsLsSzzy+2ylk4Wd1t/s8mzTwOZGkYtf4q1FFnmTgRIm6KyCxJ4rpgK2WkEM9meyI1EqPb3oYxMTl7xpIREY4q
rwTopVWOvTA/FrSX3rHlQJR8Eu+YqJC+HeWrODt6nulfms9RdgLY0zHWYj5ev9c61ijJmUDGWTWCmg==
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
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: <https://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>
X-Mailman-Original-Message-ID: <ZQfdOvw2DoOsctRx@cskk.homeip.net>
X-Mailman-Original-References: <OS3P286MB0740BDD307B9BEB732BD2256A5F6A@OS3P286MB0740.JPNP286.PROD.OUTLOOK.COM>
 by: Cameron Simpson - Mon, 18 Sep 2023 05:16 UTC

On 15Sep2023 10:49, scruel tao <scruelt@hotmail.com> wrote:
>```python
>>>> class A:
>... def __init__(self):
>... pass
>...
>```
>
>On many books and even the official documents, it seems that many authors prefer to call `__init__` as a "method" rather than a "function".
>The book PYTHON CRASH COURSE mentioned that "A function that’s part of a class is a method.", however, ` A.__init__` tells that `__init__` is a function...

As mentioned, methods in Python _are_ functions for use with a class.

>>>> A.__init__
><function A.__init__ at 0x0000026CFC5CCEE0>
>>>> a = A()
>>>> a.__init__
>
><bound method A.__init__ of <__main__.A object at 0x0000026CFC1BB400>>
>I wonder how can I call `__init__` as? Consider the output above.
>Maybe both are OK?

As you can see, they're both legal expressions.

The thing about `__init__` is that it's usually called automatically
which you make a new object. Try putting a `print()` call in your
`__init__` method, then make a new instance of `A`.

The purpose of `__init__` is to initialise the object's attribute/state
after the basic, empty-ish, object is made.

Like other dunder methods (methods named with double underscores front
and back) it is called automatically for you. In a subclass the
`__init__` method calls the subperclass `__init__` and then does
whatever additional things might be wanted by the subclass.

Let's look at what you used above:

>>> A.__init__
<function A.__init__ at 0x0000026CFC5CCEE0>

Here's we've just got a reference to the function you supposlied with
the class definition for class `A`.

This:

>>> a = A()
>>> a.__init__
<bound method A.__init__ of <__main__.A object at 0x0000026CFC1BB400>

Here's you've accessed the name `__init__` via an existing instance of
`A`, your variable `a`. At this point you haven't called it. So you've
got a callable thing which is a binding of the function to the object
`a` i.e. when you call it, the "bound method" knows that t is associated
with `a` and puts that in as the first argument (usually named `self`).

Cheers,
Cameron Simpson <cs@cskk.id.au>

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor