0% found this document useful (0 votes)
8 views13 pages

Abstract Base Classes: Static Data Member

Abstract Base Classes (ABCs) in Python are classes that cannot be instantiated and serve as blueprints for other classes, promoting code reusability and polymorphism. Static data members and static function members are shared across all instances of a class, with key differences in their purpose and access methods. Additionally, Python supports function overloading through optional arguments and operator overloading by redefining operator behavior in user-defined classes.

Uploaded by

Kanza Fatima
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views13 pages

Abstract Base Classes: Static Data Member

Abstract Base Classes (ABCs) in Python are classes that cannot be instantiated and serve as blueprints for other classes, promoting code reusability and polymorphism. Static data members and static function members are shared across all instances of a class, with key differences in their purpose and access methods. Additionally, Python supports function overloading through optional arguments and operator overloading by redefining operator behavior in user-defined classes.

Uploaded by

Kanza Fatima
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Abstract Base Classes

Static Data Member


Abstract Base Classes
• is a class that cannot be instantiated on its own
• is designed to be inherited by other classes.
• ABCs provide a way to define a blueprint for other classes to follow.

Benefits of Abstract Base Classes


1. Code Reusability: ABCs enable code reusability by providing a
common interface for subclasses.
2. Polymorphism: ABCs allow for polymorphism, which enables you to
treat objects of different classes in the same way.
3. Easier Maintenance: ABCs make it easier to modify or extend the
behavior of a class hierarchy.
Implementing Abstract Base Classes in Python
To define an ABC in Python, you use the abc module and the
ABCMeta metaclass.

from abc import ABCMeta, abstractmethod

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.

from abc import ABC, abstractmethod


class Printable(ABC):
@abstractmethod
def print(self):
pass
Implementing an Interface
To implement an interface,
you create a class that inherits from the interface and implements all the 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

1. We define a class MyClass with a static data member


static_data_member.
2. The static data member is assigned a string value "I am a
static data member“
3. .We access the static data member using the class name
MyClass and print its value.

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()

# Output: I am a static function member


1. We define a class MyClass with a static function member
static_function_member.
2. The static function member is defined using the @staticmethod decorator.
3. The function prints a message "I am a static function member".
4. We access the static function member using the class name MyClass and
call it.
Key Differences
Here are the key differences between static data members and static function
members:-
• Purpose: Static data members are used to store data that is shared by all
instances of a class, while static function members are used to define
functions that belong to a class.
• Syntax: Static data members are defined using the class keyword, while
static function members are defined using the @staticmethod decorator.-
• Access: Static data members can be accessed using the class name or an
instance of the class, while static function members can only be accessed
using the class name.
Function Overloading
Function overloading is a feature that allows multiple functions with the same
name to be defined, but with different parameter lists.
Python's Approach to Function Overloading
Python does not support function overloading in the classical sense.
However, you can achieve similar behavior using optional arguments or variable-
length argument lists.

def greet(name, msg="Hello"):


print(f"{msg}, {name}!")

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

You might also like