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()

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.



------ Original Message ------
From: "scruel tao via Python-list" <>
To: "" <>
Sent: Friday, 15 Sep, 23 At 11:49
Subject: Why doc call `__init__` as a method rather than function?
class A:
...   def __init__(self):
...     pass
<function A.__init__ at 0x0000026CFC5CCEE0>
a = A()
<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!
-- <>

-- <br>Anthony Flury<br>

Reply via email to