Why doc call `__init__` as a method rather than function?
2QdxY4RzWzUUiLuE at potatochowder.com
2QdxY4RzWzUUiLuE at potatochowder.com
Fri Sep 15 09:34:22 EDT 2023
On 2023-09-15 at 10:49:10 +0000,
scruel tao via Python-list <python-list at 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.
More information about the Python-list
mailing list