Code Monkey home page Code Monkey logo

ahemdmahmoud / oop_dunder_py Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 11 KB

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

Python 100.00%
encapsulation-protocol inheritance-examples java object-oriented-programming paradigms polymorphism polymorphism-and-abstraction programming-language python3

oop_dunder_py's Introduction

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.

oop_dunder_py's People

Contributors

ahemdmahmoud avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.