[go: up one dir, main page]

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

‏‏‏‏‏‏‏‏‏‏‏‏Lecture 8

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 11

OOP in Python Eng.

Omar Yahya

Lecture 8

Abstract Methods
Abstract methods are methods that are declared in a base class but not defined (contain no
implementation). They act as placeholders that must be overridden by subclasses that inherit from
the base class. Abstract methods enforce a consistent interface for subclasses and ensure that they
provide their own implementation of the method.
A class that contains one or more abstract methods is called an abstract class. We use an abstract
class while we are designing large functional units or when we want to provide a common interface
for different implementations of a component.
To create an abstract method in Python, you need to import the abstract base class (ABC) module
and use the @abstractmethod decorator.
Abstract methods in object-oriented programming (OOP) have great benefits, especially when
working on large and complex projects.

What is the Purpose of Abstract Methods?


The primary purpose of abstract methods is to provide a way to define an interface or a contract
that must be implemented by any subclass. They are used to ensure that any class that inherits from
an abstract class implements the required methods. This is particularly useful when you want to
ensure that a class provides a specific set of methods, but you don't care about the implementation
details.

How to Define Abstract Methods in Python?


In Python, abstract methods are defined using the abc (Abstract Base Classes) module. To define
an abstract method, you need to use the abstractmethod decorator from the abc module.
Here is an example:
Example 1:

# Python program showing


# abstract base class work
from abc import ABC, abstractmethod

class Polygon(ABC):

1
OOP in Python Eng. Omar Yahya

@abstractmethod
def no_of_sides(self):
pass

class Triangle(Polygon):

# overriding abstract method


def no_of_sides(self):
print("I have 3 sides")

class Pentagon(Polygon):

# overriding abstract method


def no_of_sides(self):
print("I have 5 sides")

class Hexagon(Polygon):

# overriding abstract method


def no_of_sides(self):
print("I have 6 sides")

class Quadrilateral(Polygon):

# overriding abstract method


def noofsides(self):
print("I have 4 sides")

# Driver code
R = Triangle()
R.no_of_sides() # Output=> I have 3 sides

K = Quadrilateral()
K.no_of_sides() # Output=> I have 4 sides

2
OOP in Python Eng. Omar Yahya

R = Pentagon()
R.no_of_sides() # Output=> I have 5 sides

K = Hexagon()
K.no_of_sides() # Output=> I have 6 sides

This code defines an abstract base class called “Polygon” using the ABC (Abstract Base Class)
module in Python. The “Polygon” class has an abstract method called “no_of_sides” that
needs to be implemented by its subclasses.
There are four subclasses of “Polygon” defined: “Triangle,” “Pentagon,” “Hexagon,” and
“Quadrilateral.” Each of these subclasses overrides the “no_of_sides” method and provides
its own implementation by printing the number of sides it has.
In the driver code, instances of each subclass are created, and the “no_of_sides” method is
called on each instance to display the number of sides specific to that shape.

Benefits of Abstract methods


1. Enforcing Standardization
When you have a set of classes that must share a certain behavior, abstract methods enforce a
standardization of structures. This means that all classes that inherit from the abstract class must
implement the abstract method, ensuring that each subclass has a standardized method that
performs a certain function.
Benefit: This standardization ensures that programmers follow the same basic rules when
developing new pieces of software, reducing chaos and making it easier for the team to work
together.

2. Separation of design and implementation


In large systems, you may need to specify what classes should do without specifying how to do it.
Abstract methods allow you to design a uniform interface for classes, leaving the finer details of
implementation to each subclass.
Benefit: This allows for greater flexibility in development as different developers can focus on
implementing different parts without worrying about how to implement the rest.

3. Encouraging Scalability
In large systems, you need to create a system that can be easily expanded. By using abstract classes
and abstract methods, you can design a flexible system that can be expanded by adding more
subclasses later without having to significantly modify the existing code.

3
OOP in Python Eng. Omar Yahya

Benefit: Makes it easier to add new features or new types of objects in the future without affecting
the existing system.

4. Enhance Reusability
Abstract classes can be used as a basis for designing multiple objects that share some basic
behavior but differ in detail. This enhances code reusability and allows new solutions to be created
based on old abstract classes.
Benefit: Reduce code duplication and increase maintenance and development efficiency.

5. Facilitating Maintenance and Development


In large software, maintenance becomes more complex. With abstract methods, teams can clearly
divide tasks. Abstract classes provide an overview of how the system is organized, making it easier
for new developers to understand the code and future maintenance.
Benefit: Reduce errors during maintenance and improve team efficiency.

6. Support for Polymorphism


By using abstract classes and methods, developers can interact with different objects using the
same interface, making polymorphism easier to use. This allows objects from different classes to
be used in the same context as long as they share the same abstract method.
Benefit: Improved flexibility when using different types of objects.

7. Improved Maintainability
By unifying and separating design and implementation, it is easier for programmers to understand
how the system works and maintain it. Any change to the system can be made more organized and
without having to examine every piece of code.
Benefit: Reduced errors in future modifications and improved maintenance efficiency.

8. Flexibility
Abstract methods provide a great deal of flexibility to developers, as they can focus on the overall
structure and think about the design of the abstract class first, and then leave the implementation
to the details of each subclass. This makes the system more dynamic and adaptable to new
requirements.
Benefit: Enables modification and addition to the system without disrupting the underlying
structures.

4
OOP in Python Eng. Omar Yahya

9. Facilitating Teamwork
In large programming teams, where multiple programmers work on different parts of a project,
abstract methods set clear standards that everyone can follow. This allows developers to work on
implementing different parts of the program without having to know the intricacies of other parts.
Benefit: Facilitating collaboration between team members and ensuring that the resulting code is
compatible and consistent.

10. Reducing Potential Errors


Abstract methods force programmers to implement necessary functions in subclasses, reducing the
chance of forgetting some vital parts of the program.
Benefit: Increased system reliability and ensuring that all essential functions are implemented.

Common Use Cases for Abstract Methods in Python


Abstract methods are commonly used in the following scenarios:
1. API Design
Abstract methods are useful when designing APIs that require a specific interface to be
implemented.

2. Plugin Architecture
Abstract methods are useful when creating a plugin architecture where plugins need to implement
a specific interface.

3. Domain Modeling
Abstract methods are useful when modeling complex domains and ensuring that certain methods
are implemented by all classes in the domain.

Utilization of Abstract Methods in Complex Software Systems


Large-scale programs are large, complex systems that serve millions of users or process vast
amounts of data, often distributed across multiple servers or geographic regions. These programs
require high organization and complexity in design to ensure performance and efficiency, which
is why concepts such as abstract methods are used to standardize and organize the internal
structure. Examples of large programs that might use abstract methods:

1. Enterprise Management Systems


Examples: SAP, Oracle ERP

5
OOP in Python Eng. Omar Yahya

These systems are used to manage all aspects of business operations in large companies such as
finance, human resources, manufacturing, and sales. Due to the complexity and multi-functionality
of these systems, abstract classes are used to organize and implement the different modules in a
uniform manner.
2. E-commerce Platforms
Examples: Amazon, eBay, Alibaba
These systems require a complex architecture to handle millions of users and sellers, as well as
purchase orders and shipments. Abstract classes are used to organize processes such as order
processing, payment, inventory management, and customer service.

3. Content Management Systems (CMS)


Examples: WordPress, Joomla, Drupal
These systems need to manage a wide range of content types, where abstract methods are used to
ensure that all sub-components such as articles, images, and documents follow the same structure
and interface to deal with them.

4. Cloud Computing Systems


Examples: AWS (Amazon Web Services), Microsoft Azure, Google Cloud
Abstract classes are used in these systems to ensure that the services provided such as storage,
computing, networking, and databases are uniform, so that each service can provide a consistent
interface to users despite differences in implementation details.

5. Banking Systems
Examples: Bank account management systems, credit cards, money transfers
These systems need to handle sensitive data and provide complex services in a uniform and secure
manner. Abstract classes are used to ensure that different banking operations such as payment
processing, account management, and identity verification follow the same structural principles.

6. Social Media Platforms


Examples: Facebook, Twitter, Instagram
Managing giant social networks requires advanced structure to organize dealing with millions of
users and diverse content (such as posts, photos, videos). Abstract classes help unify the interfaces
for dealing with each type of content and users.

7. Database Management Systems (DBMS)

6
OOP in Python Eng. Omar Yahya

Examples: MySQL, PostgreSQL, Oracle DB


These systems process huge amounts of data and need standardized ways to handle basic
operations such as save, query, update, and delete. Abstract classes are used to standardize how
these operations are performed across different types of databases.

__name__ Variable
__name__ in Python is a special magic variable that is automatically defined when a Python file
is executed. This variable depends on the context in which the file is being run, and determines
how the program will behave. If the file is run directly as a standalone program, __name__ will
take the value "__main__". However, if this file is imported into another program as a module, the
value of __name__ will be the name of the imported file or module, without the .py extension.
This behavior allows programmers to control the execution of code based on how the file is called.
When writing a Python file, the intent may be to run it directly to execute a specific program, or
to use it as part of another program. This is where __name__ comes in, which serves as a clue to
the context in which the code is being run. Using the conditional statement if __name__ ==
"__main__", a programmer can write specific code that will only execute when the file is run as
the main program, and not when it is imported into another file. This gives programmers the ability
to have their files contain multiple functions; it could be a file that is run directly, or a module that
can be imported and its components used elsewhere without having to execute code that is of no
use when the file is imported.

How is it used?
__name__ is mainly used in Python files to check whether the file is being run as the main program
or imported as part of another program.

if __name__ == "__main__":
# Code that only runs if the file is run
directly.
A. When running the file directly:
If you run a Python file directly from the command line or the development environment, Python
will assign the variable __name__ the value "__main__". This way, the file can determine
whether some code (such as main functions or performing some action) should be run or not.
B. When importing the file as part of a module:
If you import the file in another file using import, Python will not assign the variable __name__
the value "__main__", but will give it the file name without the .py extension. This allows you
to write reusable modules without running special code on import.

7
OOP in Python Eng. Omar Yahya

Let's take an example to illustrate this. Let's say you have a file that contains functions that perform
certain tasks, such as calculations or data manipulation. In this case, you may need to write some
code to test these functions when developing the file, but you don't want this code to be executed
when the file is imported as a module into another program. This is where __name__ comes in.
You can put these test or executable codes under the conditional statement if __name__ ==
"__main__", which ensures that they will only be executed if the file is run directly.
The biggest advantage of this approach is that it separates the execution logic from the module
logic. The file can be an independent module that can be imported and reused in many other
projects without running code that is not necessary when it is imported. It also allows programmers
to avoid problems that may occur when trying to run unnecessary or test code inside a file that is
imported as a module.
For example, if you have a file that contains a function to greet the user, and you want this function
to greet the user only when the file is run directly and not when it is imported, you can use
__name__ to control the behavior of the program. When the file is run directly, the code is
executed, but when it is imported, the underlying code for the greeting is not executed, and the file
is only available for use as part of a larger program. In this way, __name__ is a powerful and
flexible tool that helps Python programmers organize their code and ensure that unnecessary code
is not executed when importing files as modules, while at the same time enabling them to execute
necessary code when running the file as a standalone program.

Main Function
In Python, there is no built-in main() function like in some other programming languages like
C++ or Java, where there is a fixed starting point for the program. However, in Python, you can
define a function called main() and use it as the starting point for your program when needed,
along with the conditional statement if __name__ == "__main__".

What is the main() function in python?


The main() function in Python is not a special or built-in function in the language, but rather a
function that the programmer writes to organize the code. The idea is that you write the main code
that you want to execute inside the main() function, and then use the conditional statement:

if __name__ == "__main__":
main()
Why do we use main() with if __name__ == "__main__"?
The reason for using this approach is:
• Code organization: Putting the main code in the main() function makes the code more
organized and easier to understand.

8
OOP in Python Eng. Omar Yahya

• Reuse: If you import the file into another program, the code inside if __name__ ==
"__main__" will not be executed. Thus, you ensure that the main() function is executed
only when the file is run directly.
• Maintenance improvement: When you need to modify the code, you will find that
separating the functions inside the main() function makes the program easier to work with.
Example 2:

def main():
print("Hello, this is the main function.")
# Put here any code you want to run when the program
starts.

if __name__ == "__main__":
main()
When is using main() useful?
• When writing large programs: If you are working on a large project with a lot of code,
breaking the program into multiple functions including a main() function helps organize
the program and makes it easier to maintain.
• When writing importable modules: If you intend to use the same file as a standalone
program and also as an import module, using main() you can control whether the code is
executed when it is imported.

Programmers who use the main() function in Python, with the statement if __name__ ==
"__main__", benefit from the code organization and pattern similar to traditional languages like
Java and C++, where the main() function is the fixed starting point of the program.

Input Validation Objects


Input Validation in object-oriented programming (OOP) refers to the process of designing objects
(typically classes) that ensure user inputs meet specific criteria before being accepted or processed.
This is an important aspect of robust software development as it helps in preventing errors,
unexpected behavior, or security vulnerabilities due to improper input. For example, assume a
program displays a menu that allows the user to select items A, B, C, or D. The program should
validate any character entered by the user and only accept one of these four letters.

9
OOP in Python Eng. Omar Yahya

Example 3:

class MenuValidator:
def __init__(self, valid_options):
# Store the valid options (e.g., 'A', 'B', 'C', 'D')
self.valid_options = valid_options

def validate_input(self, user_input):


"""
Validate if the input is one of the allowed options.
"""
# Check if the input is in the valid options
if user_input.upper() in self.valid_options:
return True
else:
return False

# Example usage
def main():
# Create an instance of the MenuValidator class with valid
options
validator = MenuValidator(valid_options=['A', 'B', 'C',
'D'])

# Get user input


user_input = input("Please select an option (A, B, C, D):
")

# Validate the input


if validator.validate_input(user_input):
print(f"Valid option selected: {user_input.upper()}")

10
OOP in Python Eng. Omar Yahya

else:
print(f"Invalid option: {user_input}. Please try
again.")

if __name__ == "__main__":
main()
# ↓↓-Output-↓↓
# Please select an option (A, B, C, D): a
# Valid option selected: A

11

You might also like