Skip to content

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

Notifications You must be signed in to change notification settings

AhemdMahmoud/OOP_dunder_PY

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 

Repository files navigation

OOP_PY_Training

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

continue with me about:

magic-methods-in-python

Dunder Functions

  • The term "dunder" is a colloquial abbreviation for "double underscore." In Python, dunder functions refer to special methods or functions that are enclosed within double underscores on both sides of their names. These methods provide a way to define specific behaviors for objects of a class and are also known as magic methods or special methods.

  • Dunder functions in Python are predefined names that have a specific meaning to the interpreter. They allow you to define how instances of your class should behave in various contexts, such as arithmetic operations, string representation, comparisons, and more. By implementing dunder functions, you can customize the behavior of your objects and make them act like built-in Python types.

0 (1)

Here are a few examples of commonly used dunder functions:

__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__

This function allows you to define the behavior of the len() function when applied to an object.

__add__, __sub__, __mul__, etc

1_X1M3xNDVfulTLcVvdB6Anw

These functions define the behavior of arithmetic operations for objects, such as addition, subtraction, multiplication, and others.

The benefits of using dunder functions include:

  • Customizing object behavior: Dunder functions enable you to define how instances of your class should behave in specific situations. This customization provides flexibility and allows you to create objects that can interact with Python's built-in functionality.

  • Enhancing readability: By implementing dunder functions, you can make your code more readable and intuitive. For example, defining __str__ and __repr__ methods can provide a clear and informative string representation of your objects when printing or debugging.

  • Leveraging Python's ecosystem: Python's standard library and various third-party libraries often rely on specific dunder functions. By implementing these functions in your own classes, you can take advantage of existing functionality and seamlessly integrate your objects with other Python code.

  • Operator overloading: Dunder functions related to arithmetic, 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 to combine instances of your class.

About

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

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages