0

[SOLVED]Short explanation for and methods starts with underscore like( __init__)

Can we still get output without using this method

21st Mar 2021, 4:40 PM
Goku
Goku - avatar
6 Respuestas
+ 3
Goku Magic methods or dunder are just used for operation overloading for costumized functions. Example __add__ will get called automatically when you add two or more objects of that class, then inside that __add__ function, you can costumize some functions. e.g. For two vectors you may want to add each attribute (x and y) to other attribute (x and y) of each vector object. So they are not really required or mandatory for creating a class but it can help you in some or many ways. BTW __init__ is the most common dunder method, but contrary to other statements that it is required, you can have not one in some cases depending on your needs. 🔹Class with __init__ and objects 🔹Class without __init__ but has objects 🔹Class without __init__ and no objects. ℹ️Explanations Inside I hope this is helpful. If this is still confusing, please feel free to ask. Thanks! https://code.sololearn.com/cj24iQ1th7Hf/?ref=app https://code.sololearn.com/ctho7o7rTPes/?ref=app https://code.sololearn.com/cJylYwPLdFtY/?ref=app
21st Mar 2021, 5:19 PM
noteve
noteve - avatar
+ 2
__init__ means initialize, (it works just like a constructor). It is automatically called when an object was created. You can have a class without it. But your class won't have object attributes that identifies and defines each object of your class.
21st Mar 2021, 4:48 PM
noteve
noteve - avatar
+ 2
Magic methods are special methods that you can define to add ‘magic’ to your classes. They are always surrounded by double underscores, for example, the __init__ and __str__ magic methods. For more info check 👇 "Magic Methods in Python, by example | by Stephen Fordham | Towards Data Science" https://towardsdatascience.com/magic-methods-in-python-by-example-16b6826cae5c#:~:text=Magic%20methods%20are%20special%20methods,Python's%20built%2Din%20syntax%20features.
21st Mar 2021, 4:50 PM
!Derrickee
!Derrickee - avatar
+ 1
Double underscore or in short dunderscores appearing before and after a method name implies that it a special method or so called magic method. Each of these magic methods have certain functionalities like __init__ constructs an object, __del__ destroys an object, __add__ , __sub__ , etc. are used in operator overloading. __init__ is the most important one of all as it constructs the object. If you don't use it you can still have the class but no object can be created.
21st Mar 2021, 4:55 PM
Abir Hasan
Abir Hasan - avatar
+ 1
David Ashton Can you give eg with and without these methods
21st Mar 2021, 4:59 PM
Goku
Goku - avatar
0
Whenever a variable(object) is assigned to a class(instance), __init__ is called and the data of the class is copied into the variable object. E.g. We have class named "Animal" and would like to create an object as variable named "cat", then, cat = Animal() #__init__ is called and data is copied to cat. Then if cat is added to or multiplied like: x = cat + 4 Then __add__ method is called if it has one. And same applies for other too.
21st Mar 2021, 6:07 PM
Valmob101
Valmob101 - avatar