Python OOP
  • Python OOP
  • OOP class তৈরী
  • ক্লাসে প্রপার্টি যোগ করা
  • ক্লাসে মেথড যোগ করা
  • অবজেক্ট তৈরী করা
  • ক্লাস ভ্যারিয়েবল
  • ইন্সটেন্স ভ্যারিয়েবল
  • Self কীওয়ার্ড
  • অবজেক্ট এর প্রপার্টি ভ্যালু পরিবর্তন
  • অবজেক্ট এর প্রপার্টি ডিলেট করা
  • অবজেক্টকে ডিলেট করা
  • অবজেক্ট
    • প্রতিটি অবজেক্ট আলাদা
    • instance method
  • ম্যাজিক মেথড
  • Construction
  • এনক্যাপসুলেশনঃEncapsulation
  • Method
    • type()
    • Static Method
    • Specials Method
      • __str__ ()
      • __add__()
      • __eq()__
  • Inheritance
    • child class তৈরী
    • চাইল্ড অবজেক্ট প্যারেন্ট অবজেক্ট এর উত্তরাধিকার
    • super()কীওয়ার্ড
      • চাইল্ড ক্লাস হতে প্যারেন্ট ক্লাসের প্রপার্টি এক্সেস করা
      • চাইল্ড ক্লাসের নিজস্ব প্রোপার্টি এবং মেথড
    • isinstance()
      • চাইল্ড ক্লাসে প্যারেন্ট ক্লাসের মেথড কল করা
    • issubclass()
    • Method Overriding
    • super().__init__()
  • অ্যাবস্ট্রাকশন (Abstruction)
Powered by GitBook
On this page
  1. Method
  2. Specials Method

__add__()

Let's add two variables, x and y:

 x = 2
 y = 3
 x + y
 5

When Python executed this code, it called the __add__ method on x, passing it y:

>>> x.__add__(y)
5

The add method is called when using the + operator. We can define a custom add method for our class.

p1 + p2 is equal to p1._add__(p2)

class Person:
    def __init__(self, name, age):
      self.name = name
      self.age = age
    def __add__(self,other)  :
      age = self.age+other.age
      return age


person1 = Person('olee',25)      
person2 = Person('mim',18)   

print(person1+person2)

OutPut

43

Previous__str__ ()Next__eq()__

Last updated 2 years ago