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

Clara Spealman cspealma at redhat.com
Fri Sep 15 11:16:32 EDT 2023


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 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 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 at redhat.com  M: +1.336.210.5107
[image: https://red.ht/sig] <https://red.ht/sig>
TRIED. TESTED. TRUSTED. <https://redhat.com/trusted>


More information about the Python-list mailing list