I was impressed by reading Mr. Masuda's How to write "case classification". It all started with trial and error, wondering if it could be done with Python. I intended to keep it in my own memo, but I thought that some people might have similar troubles, I decided to post.
As an example, consider the code to find the area of a shape.
def calculate_area(shape_type, shape_info):
if shape_type == 'triangle':
return shape_info['bottom'] * shape_info['height'] / 2
if shape_type == 'rectangle':
return shape_info['bottom'] * shape_info['height']
I'm not happy that the number of if statements in the function increases as the number of types of shapes increases. (This is a simple example, but it will be difficult to fix if the if is nested)
Let's prepare a class for each figure as a preliminary preparation.
from abc import ABCMeta, abstractmethod
class Shape(metaclass=ABCMeta):
def __init__(self, shape_info):
self.shape_info = shape_info
@abstractmethod
def calculate_area(self):
pass
class Triangle(Shape):
def calculate_area(self):
return self.shape_info['bottom'] * self.shape_info['height'] / 2
class Rectangle(Shape):
def calculate_area(self):
return self.shape_info['bottom'] * self.shape_info['height']
We have prepared Triangle class and Rectangle class. However, if this is all, which class is instantiated with if for each shape_type when calling It seems that it will be necessary to separate the cases.
if shape_type == 'triangle':
shape = Triangle(shape_info)
if shape_type == 'rectangle':
shape = Rectangle(shape_info)
By using Enum, shape_type and class name can be linked, You can use eval to execute the contents of a string as it is in Python syntax.
class ShapeType(Enum):
triangle = 'Triangle'
rectangle = 'Rectangle'
@classmethod
def get_shape_instance(cls, shape_type, shape_info):
return eval(cls[shape_type].value)(shape_info)
If you write it like this, you don't need to be aware of the implementation class. You can use ShapeType like an interface. When calling, it will be as follows.
shape = ShapeType.get_shape_instance(shape_type, shape_info)
Because this is my first post and I am still studying There may be some deficiencies in the text, but please forgive me. I hope this article helps someone.
Recommended Posts