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 functionsrefer
to special methods or functions that are enclosed within double underscores on both sides of their names. These methods provide a way todefine specific behaviors
for objects of a class and are also known asmagic methods
orspecial methods
. -
Dunder functions in Python are
predefined names
that have aspecific meaning
to the interpreter. They allow you to definehow instances
of your class shouldbehave in various contexts
, such asarithmetic operations
,string representation
,comparisons
, and more. By implementing dunder functions, you cancustomize
thebehavior of your objects
and 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 operators
allow 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 instances
of your class.