Abstract Base Classes: Static Data Member
Abstract Base Classes: Static Data Member
class AbstractClassExample(metaclass=ABCMeta):
@abstractmethod
def do_something(self):
pass
Abstract Base Classes and file handling are powerful tools in
Python that can help you create robust, maintainable, and efficient
software systems.
By combining ABCs and file handling, you can create a flexible and
scalable file handling system that meets your specific needs.
# Interfaces
are abstract classes that only define method signatures, without providing any
implementation.
They are used to specify a contract that must be implemented by any class that
implements the interface.
Defining an Interface
To define an interface in Python,
you can use the abc module and define a class with abstract methods.
class Document(Printable):
def __init__(self, content):
self.content = content
def print(self):
print(self.content)
Note that Python does not have a built-in concept of interfaces like some other
languages. However, you can use abstract base classes to achieve similar
functionality.
In summary:-
• Abstract base classes define a blueprint for other classes to follow and
can provide some implementation.-
• Interfaces define a contract that must be implemented by any class that
implements the interface and do not provide any implementation.-
• Both abstract base classes and interfaces are used to promote code
reusability and modularity.
Static Data Member
Static data members are variables that are shared by all instances of a
class.
They are defined inside the class definition, but outside any method
definition.
class MyClass:
static_data_member = "I am a static data member“
print(MyClass.static_data_member)
# Output: I am a static data member
Note that static data members are shared by all instances of the
class. If we create multiple instances of MyClass, they will all
share the same static_data_member value.
Static Function Members
Static function members are functions that belong to a class, rather than
an instance of the class.
They are defined using the
@staticmethod decorator.class
MyClass:
@staticmethod
def static_function_member():
print("I am a static functionmember")
MyClass.static_function_member()
greet("John")
# Output: Hello, John!
greet("Jane", "Hi")
# Output: Hi, Jane!
Operator Overloading
Operator overloading is a feature that allows you to redefine the behavior of
operators such as +, -, *, etc.
when working with user-defined classes.
Basic Example of Operator Overloading
class Vector:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, other):
return Vector(self.x + other.x, self.y + other.y)
v1 = Vector(2, 3)
v2 = Vector(4, 5)
v3 = v1 + v2 # Output: Vector(6, 8)
List of Overloadable Operators
Here's a list of operators that can be overloaded in Python:-
• Arithmetic operators: +, -, *, /, //, %, **-
• Comparison operators: ==, !=, <, >, <=, >=-
• Assignment operators: =, +=, -=, *=, /=, //= , %= , **=-
• Logical operators: &, |, ^, ~-
• Bitwise shift operators: <<, >>-
• Membership test operators: in, not in