from abc importABC, abstractmethodclassVehicle(ABC):@abstractmethoddefmax_speed(self):passclassCar(Vehicle):@propertydefmax_speed(self):return200car =Car()print(f"Car's max speed: {car.max_speed} km/h")# Output: 200 km/h
Example 2: Basic usage
ABCMeta
定義 Abstract Base Classes (ABCs) 的 metaclass。 Python 中的 metaclass 是 class 的 class,它定義了 class 的行為方式。 abc.ABCMeta 提供了在 ABC 中定義 abstract 方法和屬性的機制。
如果不想繼承 ABC,可以透過在 Class 中設定 metaclass=ABCMeta ,來建立 Abstract Base Class。
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self):
pass
@abstractmethod
def perimeter(self):
pass
class Rectangle(Shape):
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
return self.width * self.height
def perimeter(self):
return 2 * (self.width + self.height)
# Trying to instantiate Shape will raise an error
# shape = Shape()
# Working with a concrete implementation
rect = Rectangle(10, 20)
print(f"Rectangle area: {rect.area()}") # Output: 200
print(f"Rectangle perimeter: {rect.perimeter()}") # Output: 60
PYTHON
abc.ABCMeta
PYTHON
from abc import ABCMeta, abstractmethod
class Graphic(metaclass=ABCMeta):
@abstractmethod
def draw(self):
pass
class Circle(Graphic):
def draw(self):
print("Drawing a circle")
# Trying to instantiate Graphic will raise an error
# graphic = Graphic()
# Working with a concrete implementation
circle = Circle()
circle.draw() # Output: Drawing a circle
PYTHON
from abc import ABCMeta, abstractmethod, abstractproperty
class Vehicle(metaclass=ABCMeta):
@abstractproperty
def max_speed(self):
pass
class Car(Vehicle):
@property
def max_speed(self):
return 150
car = Car()
print(f"Car's max speed: {car.max_speed} km/h")
PYTHON
@ABC.register
PYTHON
ABC.register(Subclass)
PYTHON
from abc import ABC, abstractmethod
class MyABC(ABC):
@abstractmethod
def do_something(self):
pass
@MyABC.register
class ConcreteClass:
@property
def do_something(self):
return "Doing something in ConcreteClass"
print(issubclass(ConcreteClass, MyABC)) # Output: True
print(issubclass(ConcreteClass, ABC)) # Output: True
x = ConcreteClass()
print(x.do_something) # Output: Doing something in ConcreteClass
PYTHON
from abc import ABC, abstractmethod
class MyABC(ABC):
@abstractmethod
def do_something(self):
pass
class AnotherClass:
@property
def do_something_else(self):
return "Doing something in ConcreteClass"
# Registering using method call syntax
MyABC.register(AnotherClass)
# AnotherClass is now considered a subclass of MyABC
print(issubclass(AnotherClass, MyABC)) # Output: True
print(issubclass(AnotherClass, ABC)) # Output: True
x = AnotherClass()
print(x.do_something_else) # Output: Doing something in ConcreteClass
PYTHON
from abc import ABC, ABCMeta, abstractmethod
# Defining the ABC
class MyAbstractBaseClass(metaclass=ABCMeta):
@abstractmethod
def my_method(self):
pass
# Registering a virtual subclass
class UnrelatedClass:
def my_method(self):
print("Implementation in UnrelatedClass")
MyAbstractBaseClass.register(UnrelatedClass)
print(issubclass(UnrelatedClass, MyAbstractBaseClass)) # Outputs: True
print(issubclass(MyAbstractBaseClass, ABC)) # Outputs: False
print(issubclass(MyAbstractBaseClass, ABCMeta)) # Outputs: False
# =========================================================
# Defining the ABC
class MyAbstractBaseClass(ABC):
@abstractmethod
def my_method(self):
pass
# Registering a virtual subclass
class UnrelatedClass:
def my_method(self):
print("Implementation in UnrelatedClass")
MyAbstractBaseClass.register(UnrelatedClass)
print(issubclass(UnrelatedClass, MyAbstractBaseClass)) # Outputs: True
print(issubclass(MyAbstractBaseClass, ABC)) # Outputs: True
print(issubclass(MyAbstractBaseClass, ABCMeta)) # Outputs: False
PYTHON
import cv2
import numpy as np
from abc import ABC, abstractmethod
class ImageProcessor(ABC):
@abstractmethod
def process(self, image: np.ndarray) -> np.ndarray:
"""Process an image and return the result."""
pass
class GrayscaleConverter:
def process(self, image: np.ndarray) -> np.ndarray:
return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Registering with the abstract base class
ImageProcessor.register(GrayscaleConverter)
class ImageBlurrer:
def process(self, image: np.ndarray) -> np.ndarray:
return cv2.GaussianBlur(image, (5, 5), 0)
# Registering with the abstract base class
ImageProcessor.register(ImageBlurrer)
# Checking if the classes are registered as subclasses
print(issubclass(GrayscaleConverter, ImageProcessor))
print(issubclass(ImageBlurrer, ImageProcessor))
# Example usage
image = cv2.imread('113833397_p0.jpg')
grayscale_converter = GrayscaleConverter()
blurrer = ImageBlurrer()
gray_image = grayscale_converter.process(image)
blurred_image = blurrer.process(image)
PYTHON
@abc.abstractmethod
PYTHON
import abc
import math
class Shape(abc.ABC):
@abc.abstractmethod
def area(self):
"""Method to calculate the area of the shape."""
pass
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return math.pi * self.radius ** 2
class Rectangle(Shape):
def __init__(self, length, width):
self.length = length
self.width = width
def area(self):
return self.length * self.width
def print_area(shape):
print(f"The area of the {shape.__class__.__name__} is {shape.area()}")
# Create instances and use them
circle = Circle(5)
rectangle = Rectangle(4, 6)
print_area(circle) # Output: The area of the Circle is 78.539
print_area(rectangle) # Output: The area of the Rectangle is 24
PYTHON
import abc
class ElectronicDevice(abc.ABC):
@abc.abstractmethod
def turn_on(self):
pass
@abc.abstractmethod
def turn_off(self):
pass
@property
@abc.abstractmethod
def is_on(self):
pass
class Television(ElectronicDevice):
def __init__(self):
self._is_on = False
def turn_on(self):
if not self._is_on:
self._is_on = True
print("Television is now ON.")
def turn_off(self):
if self._is_on:
self._is_on = False
print("Television is now OFF.")
@property
def is_on(self):
return self._is_on
# Function to demonstrate usage
def test_device(device):
print(f"Is the device on? {device.is_on}")
device.turn_on()
print(f"Is the device on? {device.is_on}")
device.turn_off()
print(f"Is the device on? {device.is_on}")
# Create an instance and use it
tv = Television()
test_device(tv)
Is the device on? No
Television is now ON.
Is the device on? Yes
Television is now OFF.
Is the device on? No
PYTHON
import abc
class Vehicle(abc.ABC):
@property
@abc.abstractmethod
def number_of_wheels(self):
"""Abstract property for the number of wheels in a vehicle."""
pass
class Car(Vehicle):
@property
def number_of_wheels(self):
return 4
class Motorcycle(Vehicle):
@property
def number_of_wheels(self):
return 2
# Testing the implementation
car = Car()
motorcycle = Motorcycle()
print(f"A car has {car.number_of_wheels} wheels.")
print(f"A motorcycle has {motorcycle.number_of_wheels} wheels.")
# Output: A car has 4 wheels..
# Output: A motorcycle has 2 wheels.
PYTHON
import abc
class Computer(abc.ABC):
@abc.abstractmethod
def processor(self):
"""Abstract method to define the type of processor."""
pass
def boot(self):
"""Concrete method to boot the computer."""
print(f"Booting up with a {self.processor()} processor.")
class Laptop(Computer):
def processor(self):
return "Intel Core i7"
class Server(Computer):
def processor(self):
return "AMD Ryzen 9"
# Testing the implementation
laptop = Laptop()
server = Server()
laptop.boot() # Output: Booting up with a Intel Core i7 processor.
server.boot() # Output: Booting up with a AMD Ryzen 9 processor.
PYTHON
import abc
import cv2
import numpy as np
class ImageProcessor(abc.ABC):
@abc.abstractmethod
def process(self, image):
"""
Process an image and return the result.
"""
pass
def load_image(self, path):
"""
Load an image from a given path.
"""
return cv2.imread(path, cv2.IMREAD_COLOR)
def save_image(self, image, path):
"""
Save an image to a given path.
"""
cv2.imwrite(path, image)
class GaussianBlurProcessor(ImageProcessor):
def process(self, image):
"""
Apply Gaussian blur to the image.
"""
return cv2.GaussianBlur(image, (5, 5), 0)
class CannyEdgeProcessor(ImageProcessor):
def process(self, image):
"""
Apply Canny Edge Detection to the image.
"""
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 100, 200)
return edges
def main():
input_path = '113833397_p0.jpg'
output_path_blur = 'output_blur.jpg'
output_path_edges = 'output_edges.jpg'
# Initialize processors
blur_processor = GaussianBlurProcessor()
edge_processor = CannyEdgeProcessor()
# Load image
image = blur_processor.load_image(input_path)
# Process and save images
blurred_image = blur_processor.process(image)
blur_processor.save_image(blurred_image, output_path_blur)
edge_image = edge_processor.process(image)
edge_processor.save_image(edge_image, output_path_edges)
if __name__ == "__main__":
main()