Python Tutorial
Python Variable
Python Operators
Python Sequence
Python String
Python Flow Control
Python Functions
Python Class and Object
Python Class Members (properties and methods)
Python Exception Handling
Python Modules
Python File Operations (I/O)
Operator overloading is a feature in object-oriented programming languages like Python, which allows you to redefine the behavior of built-in operators when they are applied to instances of your custom classes. By overloading an operator, you can use it with your class instances in a way that is both intuitive and specific to your class.
Here is a list of some common overloadable operators in Python and the corresponding special methods you need to implement:
Arithmetic operators:
+
: __add__(self, other)
-
: __sub__(self, other)
*
: __mul__(self, other)
/
: __truediv__(self, other)
//
: __floordiv__(self, other)
%
: __mod__(self, other)
**
: __pow__(self, other)
In-place arithmetic operators (for operations like a += b
):
+=
: __iadd__(self, other)
-=
: __isub__(self, other)
*=
: __imul__(self, other)
/=
: __itruediv__(self, other)
//=
: __ifloordiv__(self, other)
%=
: __imod__(self, other)
**=
: __ipow__(self, other)
Comparison operators:
<
: __lt__(self, other)
<=
: __le__(self, other)
>
: __gt__(self, other)
>=
: __ge__(self, other)
==
: __eq__(self, other)
!=
: __ne__(self, other)
Unary operators:
-
: __neg__(self)
+
: __pos__(self)
~
: __invert__(self)
Other miscellaneous operators:
()
(function call): __call__(self, *args, **kwargs)
[]
(indexing): __getitem__(self, index)
and __setitem__(self, index, value)
len(obj)
: __len__(self)
Here's an example of operator overloading for a simple custom class Vector
:
class Vector: def __init__(self, x, y): self.x = x self.y = y def __repr__(self): return f"Vector({self.x}, {self.y})" def __add__(self, other): if isinstance(other, Vector): return Vector(self.x + other.x, self.y + other.y) raise TypeError("Can only add Vector instances") def __eq__(self, other): if isinstance(other, Vector): return self.x == other.x and self.y == other.y return False v1 = Vector(2, 3) v2 = Vector(1, 2) v3 = Vector(3, 5) print(v1 + v2) # Output: Vector(3, 5) print(v1 == v3) # Output: True
In this example, we have implemented the __add__()
and __eq__()
special methods to overload the +
and ==
operators, respectively, for our custom Vector
class.
How to use new
in Python:
The __new__
method is responsible for creating a new instance of a class. Here's a basic example:
class MyClass: def __new__(cls, *args, **kwargs): instance = super(MyClass, cls).__new__(cls) # Custom initialization logic can be added here return instance obj = MyClass()
Customizing object creation with new
in Python:
class CustomClass: def __new__(cls, *args, **kwargs): instance = super(CustomClass, cls).__new__(cls) # Custom initialization logic based on args and kwargs return instance obj = CustomClass(arg1, arg2, kwarg1=value1)
Overriding new
for class instantiation in Python:
class CustomClass: def __new__(cls, *args, **kwargs): instance = super(CustomClass, cls).__new__(cls) # Custom initialization logic based on args and kwargs return instance obj = CustomClass(arg1, arg2, kwarg1=value1)
Practical examples of new
in Python:
(Practical examples will depend on specific use cases and requirements.)
Singleton pattern implementation with new
in Python:
class Singleton: _instance = None def __new__(cls, *args, **kwargs): if not cls._instance: cls._instance = super(Singleton, cls).__new__(cls) # Custom initialization logic return cls._instance obj1 = Singleton() obj2 = Singleton() print(obj1 is obj2) # Output: True
Metaclasses and the new
method in Python:
class CustomMeta(type): def __new__(cls, name, bases, dct): # Custom metaclass logic return super(CustomMeta, cls).__new__(cls, name, bases, dct) class MyClass(metaclass=CustomMeta): # Class definition pass
Using super()
with new
in Python:
class CustomClass: def __new__(cls, *args, **kwargs): instance = super().__new__(cls) # Custom initialization logic based on args and kwargs return instance obj = CustomClass(arg1, arg2, kwarg1=value1)
Immutable objects and the new
method in Python:
class ImmutableObject: def __new__(cls, value): instance = super(ImmutableObject, cls).__new__(cls) instance.value = value return instance obj = ImmutableObject(42)
Memory management and new
in Python:
Custom memory management logic can be added to __new__
for specific use cases.
Dynamic class creation using new
in Python:
def create_dynamic_class(class_name, base_class, attributes): return type(class_name, (base_class,), attributes) DynamicClass = create_dynamic_class("DynamicClass", object, {"attribute": "value"})