0

init method in python 3

my question is that: Is it mandatory to always type underscore twice while writing init method(like __init__)? Is that only the syntax?

14th Oct 2018, 8:32 AM
Shreya Mahajan
Shreya Mahajan - avatar
3 ответов
+ 1
It is necessary because Python treats some methods with underecores differently so there aren't name conflicts with special methods and normal ones.
14th Oct 2018, 9:47 AM
TurtleShell
TurtleShell - avatar
+ 1
There are special method names to define what happens when you use built-in operators or functions on your object/class, and they start and end with two underscores. Examples: obj1 = MyClass() __init__ obj1 + obj2 __add__ del (obj2) __del__ ... and so on. https://www.python-course.eu/python3_magic_methods.php
14th Oct 2018, 9:53 AM
HonFu
HonFu - avatar
+ 1
Got it👍Thanks!
14th Oct 2018, 12:20 PM
Shreya Mahajan
Shreya Mahajan - avatar