Class Customization and Operator Overloading

0
11كيلو بايت

Class customization

Class customization allows you to define how a class behaves for specific operations such as printing or accessing attributes.

Customize classes by creating instances methods using special method names (double underscores).

Rich comparison methods overload some common comparison operators.

Rich comparison method

Overloaded operator

__lt__(self, other)

Less than (<)

__le__(self, other)

Less than or equal-to (<=)

__gt__(self, other)

Greater than (>)

__ge__(self, other)

Greater than or equal-to (>=)

__eq__(self, other)

Equal to (==)

__ne__(self, other)

Not equal to (!=)

 

Operator overloading

Operator Overloading allows you to replace the functionality of numeric operators (such as +, -, *, and /) with a method.

For example, you can write an __add__(self, other) method to replace the functionality of the (+) operator.

Use the built-in isinstance() method to handle different object types for the operation.

Method

Description

__add__(self, other)

Add (+)

__sub__(self, other)

Subtract (-)

__mul__(self, other)

Multiply (*)

__truediv__(self, other)

Divide (/)

__floordiv__(self, other)

Floored division (//)

__mod__(self, other)

Modulus (%)

__pow__(self, other)

Exponentiation (**)

__and__(self, other)

"and" logical operator

__or__(self, other)

"or" logical operator

__abs__(self)

Absolute value (abs())

__int__(self)

Convert to integer (int())

__float__(self)

Convert to floating point (float())

البحث
الأقسام
إقرأ المزيد
Physics
SEPTA-O-LEVEL PHYSICS SEMINAR SLATED FOR 23RD JUNE 2024 AT ST. JOSEPH OF NAZARETH HIGH SCHOOL KAVULE-KATENDE
https://acrobat.adobe.com/id/urn:aaid:sc:EU:39c1aa82-237a-43bf-ba1a-fbe5223d26f1
بواسطة Question Bank 2024-07-14 19:52:41 2 13كيلو بايت
Chemistry
UCE CHEMISTRY SCORING GUIDE
UCE CHEMISTRY SCORING GUIDE
بواسطة Landus Mumbere Expedito 2024-08-10 13:47:53 0 9كيلو بايت
التعليم
A MUST KNOW FOR A'LEVEL HISTORY STUDENTS
https://acrobat.adobe.com/id/urn:aaid:sc:EU:7c45002b-2e38-426f-a0b9-8447c474993b
بواسطة Landus Mumbere Expedito 2024-07-15 19:08:17 0 11كيلو بايت
Technology
Forms of Computer Software Attacks
Computer software attacks are malicious activities aimed at compromising the security, integrity,...
بواسطة ALAGAI AUGUSTEN 2024-07-13 17:30:11 0 9كيلو بايت
Computer Programming
Creating your first HTML file
Step-by-Step Guide 1. Choose a Text Editor: Basic Text Editors: Notepad (Windows), TextEdit...
بواسطة HTML PROGRAMMING LANGUAGE 2024-08-13 03:22:25 0 8كيلو بايت
Tebtalks https://tebtalks.com