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

anthony.flury anthony.flury at btinternet.com
Fri Sep 15 09:05:18 EDT 2023



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 at python.org>
To: "python-list at python.org" <Python-list at 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 at btinternet.com


More information about the Python-list mailing list