In Python, Object-Oriented Programming (OOP) is a programming paradigm that allows you to structure your code around objects, which are instances of classes. OOP provides concepts such as encapsulation, inheritance, and polymorphism. Here's an example that demonstrates these concepts in Python, Visit Here
-
The term
"dunder"is a colloquial abbreviation for"double underscore."In Python, dunder functionsreferto special methods or functions that are enclosed within double underscores on both sides of their names. These methods provide a way todefine specific behaviorsfor objects of a class and are also known asmagic methodsorspecial methods. -
Dunder functions in Python are
predefined namesthat have aspecific meaningto the interpreter. They allow you to definehow instancesof your class shouldbehave in various contexts, such asarithmetic operations,string representation,comparisons, and more. By implementing dunder functions, you cancustomizethebehavior of your objectsand make them act likebuilt-in Python types.
__init__
This function is called when an object is created from a class and is used for initializing the object's attributes.
__str__ and __repr__
These functions define the string representation of an object. str is used for informal string representation, while repr is used for a more formal representation.
__len__
__add__, __sub__, __mul__, etc
These functions define the behavior of arithmetic operations for objects, such as addition, subtraction, multiplication, and others.
-
Operator overloading: Dunder functions related toarithmetic, comparisons, and other operatorsallow you to overload operators, enabling intuitive and concise syntax for working with your custom objects. For example, by implementing__add__, you can use the+operator tocombine instancesof your class.