Mailing List Archive

Why doc call `__init__` as a method rather than function?
```python
>>> class A:
... def __init__(self):
... pass
...
>>> A.__init__
<function A.__init__ at 0x0000026CFC5CCEE0>
>>> a = A()
>>> a.__init__
<bound method A.__init__ of <__main__.A object at 0x0000026CFC1BB400>>
```

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...

I wonder how can I call `__init__` as? Consider the output above.
Maybe both are OK? If you prefer or think that we must use one of the two, please explain the why, I really want to know, thanks!
--
https://mail.python.org/mailman/listinfo/python-list
Re: Why doc call `__init__` as a method rather than function? [ In reply to ]
To me __init__ is a method, but that is implemented internally as
function associated to a class


When you use A.__init__ on it's own and inspect it, then it will show
that it is a function object - this is expected. The implementation
internals of the runtime don't need to have a special implementation for
a method.

Like all of the other __<name>__ methods you shouldn't ever need to call
them directly : these are called dunder methods and represent functions
and features which are called by other operators.

The only recommended way to call A.__init__ is to create an instance of
A : obj = A() - the __init__ method gets called automatically with a
newly created object.

If you did call A.__init__() directly on a an already existing object :

obj = A()
A.__init__(obj)

for example - all that would happen is that the object itself would be
reset : ie the obj's attributes would be reset to whatever the __init__
sets them to - if you need to do that it might be better to have a reset
method.


Regards,

Tony


------ Original Message ------
From: "scruel tao via Python-list" <python-list@python.org>
To: "python-list@python.org" <Python-list@python.org>
Sent: Friday, 15 Sep, 23 At 11:49
Subject: Why doc call `__init__` as a method rather than function?
```python
class A:
... def __init__(self):
... pass
...
A.__init__
<function A.__init__ at 0x0000026CFC5CCEE0>
a = A()
a.__init__
<bound method A.__init__ of <__main__.A object at 0x0000026CFC1BB400>>
```
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...
I wonder how can I call `__init__` as? Consider the output above.
Maybe both are OK? If you prefer or think that we must use one of the
two, please explain the why, I really want to know, thanks!
--
https://mail.python.org/mailman/listinfo/python-list
<https://mail.python.org/mailman/listinfo/python-list>

-- <br>Anthony Flury<br>anthony.flury@btinternet.com
--
https://mail.python.org/mailman/listinfo/python-list
Re: Why doc call `__init__` as a method rather than function? [ In reply to ]
On 2023-09-15 at 10:49:10 +0000,
scruel tao via Python-list <python-list@python.org> wrote:

> ```python
> >>> class A:
> ... def __init__(self):
> ... pass
> ...
> >>> A.__init__
> <function A.__init__ at 0x0000026CFC5CCEE0>
> >>> a = A()
> >>> a.__init__
> <bound method A.__init__ of <__main__.A object at 0x0000026CFC1BB400>>
> ```
>
> 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...

I always call __init__ "the initializer." YMMV.

> I wonder how can I call `__init__` as? Consider the output above.
> Maybe both are OK? If you prefer or think that we must use one of the two, please explain the why, I really want to know, thanks!

Usually, you don't call (or even refer to) __init__ from your
application. One __init__ can call another one in the case of
initializing superclasses.

When you evaluate A(), Python calls __init__ for you. You can see this
if you add something "visible" to __init__, like a print statement.
--
https://mail.python.org/mailman/listinfo/python-list
Re: Why doc call `__init__` as a method rather than function? [ In reply to ]
All methods are functions, but not all functions are methods. All methods
are functions in the namespace of a class and when resolved from the class
directly, you'll get the original function. Its only when resolved from an
*instance* of the class, as in self.__init__ or self.any_other_method(),
that the function is wrapped in a method object, which is callable and
binds the function to the particular instance passed as "self".

So the simple answer is "its both". The only slightly less simple answer is
"its both, depending on how you're getting it."

On Fri, Sep 15, 2023 at 6:53?AM scruel tao via Python-list <
python-list@python.org> wrote:

> ```python
> >>> class A:
> ... def __init__(self):
> ... pass
> ...
> >>> A.__init__
> <function A.__init__ at 0x0000026CFC5CCEE0>
> >>> a = A()
> >>> a.__init__
> <bound method A.__init__ of <__main__.A object at 0x0000026CFC1BB400>>
> ```
>
> 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...
>
> I wonder how can I call `__init__` as? Consider the output above.
> Maybe both are OK? If you prefer or think that we must use one of the two,
> please explain the why, I really want to know, thanks!
> --
> https://mail.python.org/mailman/listinfo/python-list
>
>

--

CALVIN SPEALMAN

SENIOR QUALITY ENGINEER

calvin.spealman@redhat.com M: +1.336.210.5107
[image: https://red.ht/sig] <https://red.ht/sig>
TRIED. TESTED. TRUSTED. <https://redhat.com/trusted>
--
https://mail.python.org/mailman/listinfo/python-list
Re: Why doc call `__init__` as a method rather than function? [ In reply to ]
On 15/09/2023 11:49, scruel tao via Python-list 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".

That' because in OOP terminology methods are traditionally
implemented as functions defined inside a class (There are
other ways to define methods in other languages, but the
class/function duology is by far the most common.)

What is a method? It is the way that an object handles a
message. OOP is all about objects sending messages to each
other. Many different objects can receive the same message
but they each have their own method of handling that message.
(This is called polymorphism.)

Over time the most common way to implememt OOP in a language
is to invent a "class" structure and to allow functions to
either be defined within it or added to it later. In either
case these functions are what define how a class (and its
object instances) handle a given message. So the function
describes the method for that message. And over time that
has become shortened to the function inside a class *is*
its method.

In practice, the message looks like a function call. And
the method consists of the function body (and/or any
inherited function body). Thus every method is a function
(or set of functions) but not every function is a method
(or part of one).

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


--
https://mail.python.org/mailman/listinfo/python-list
Re: Why doc call `__init__` as a method rather than function? [ In reply to ]
On Mon, 18 Sept 2023 at 13:49, anthony.flury via Python-list
<python-list@python.org> wrote:
>
>
>
> To me __init__ is a method, but that is implemented internally as
> function associated to a class
>

What is a method, if it is not a function associated with a class?

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: Why doc call `__init__` as a method rather than function? [ In reply to ]
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>
--
https://mail.python.org/mailman/listinfo/python-list
Re: Why doc call `__init__` as a method rather than function? [ In reply to ]
Op 15/09/2023 om 15:05 schreef anthony.flury via Python-list:
> Like all of the other __<name>__ methods you shouldn't ever need to
> call them directly : these are called dunder methods and represent
> functions and features which are called by other operators.
>
> The only recommended way to call A.__init__ is to create an instance
> of A : obj = A() - the __init__ method gets called automatically with
> a newly created object.
There is an exception:

  super().__init__()

to call the base class's __init__ (normally from the derived class's
__init__)

--
"Human beings, who are almost unique in having the ability to learn from the
experience of others, are also remarkable for their apparent disinclination
to do so."
-- Douglas Adams

--
https://mail.python.org/mailman/listinfo/python-list
Re: Why doc call `__init__` as a method rather than function? [ In reply to ]
Thanks for all repliers:
@Tony & @Cameron, I do know related stuffs about the dunder methods, and your explanation just make it to be more clear, thank you!
@Roel, you just caught everyone here, we do miss it even though we know it and use it regularly!

@Clara
> its both, depending on how you're getting it.
Might can be more clear: its both, depending on how you're using/getting it.

And I think I can mark this question as resolved, and with the following conclusions:
As @Clara mentioned, we need to know that "all methods are functions", so we do can call `__init__` as a method or a function, or we can be avoid to have such discussion like Dan, and call it "the initializer" (However, you will need to think about ?what is this is? for other functions :). ).
As @Alan mentioned, and according to the Wikipedia, in computer programming field, "method" is:
> A method in object-oriented programming (OOP) is a procedure associated with an object, and generally also a message. An object consists of state data and behavior; these compose an interface, which specifies how the object may be used. A method is a behavior of an object parametrized by a user.

For `__init__` and other functions in classes, we usually use them by writing code `obj.action()`, so we usually will call them as methods, so here, we call `action` or `__init__` as a method.
However, if you use them by writing code `Clz.action(obj)`, then you'd better (or must?) to call them as functions, and it is not a "daily use case" in daily development, and in some languages, this behavior won't even be possible.
**So, its kinda a "Majority rule" to call `__init__` (functions in classes) as a method.**

===
BTW, in Wikipedia, the "static methods" section is a very interesting:
> Static methods are meant to be relevant to all the instances of a class rather than to any specific instance.
This explanation might can "group" some functions back to "methods" :) However, let's still remember:
All methods are functions, but not every function is a method.

Thanks again for helping, you guys are really nice!

--
https://mail.python.org/mailman/listinfo/python-list