Two realization forms of Python polymorphism

One, Python polymorphism

1. Object polymorphism

Object polymorphism: In the inheritance system, the type at the time of definition is not the same as the type at runtime, which constitutes polymorphism at this time

The following is Python pseudo-code to achieve polymorphism in Java or C:

class A(object):
    def test(self):
        print('A test')


class B(A):
    def test(self):
        print('B test')


class C(A):
    def test(self):
        print('C test')


def go(a):
    """
    接收A类或其子类实例
    :param a:
    :return:
    """
    a.test()


if __name__ == '__main__':
    go(A())
    go(B())
    go(C())

Results of the:

It can be seen that the go function receives objects of class A or its subclasses, whether it is passing class A objects, class B objects, or class C objects, the method can be executed normally, and the polymorphism of the object is formed at this time.

 

2. Polymorphism

Class polymorphism: refers to constructing objects polymorphically through @classmethod instead of using Python's default __init__ constructor

Requirements: Implement a set of MapReduce processes to count the total number of lines of all files in the directory. The code is as follows:

"""
知识要点:
    1.接口 抽象 继承
    2.生成器
    3.map reduce
    4.多线程
    5.通过@classmethod形式批量创建对象
"""
import os
import threading


class GenericInputData(object):
    """
    通用输入抽象类 抽象方法由子类实现
    """
    def read(self):
        raise NotImplementedError

    @classmethod
    def generate_inputs(cls, config):
        raise NotImplementedError


class FileInputData(GenericInputData):
    """
    文件输入类
    """
    def __init__(self, path):
        super().__init__()
        self.path = path

    def read(self):
        return open(self.path, 'r', encoding='utf-8').read()

    @classmethod
    def generate_inputs(cls, config):
        dir_path = config['dir_path']
        for file_name in os.listdir(dir_path):
            yield cls(os.path.join(dir_path, file_name))


class GenericWorker(object):
    """
    通用Worker抽象类 抽象方法由子类实现
    """
    def __init__(self, input_data):
        self.input_data = input_data
        self.result = None

    def map(self):
        raise NotImplementedError

    def reduce(self, other):
        raise NotImplementedError

    @classmethod
    def generate_workers(cls, input_class, config):
        for input_data in input_class.generate_inputs(config):
            yield cls(input_data)


class LineCountWorker(GenericWorker):
    """
    统计文件换行符Worker类
    """
    def map(self):
        content = self.input_data.read()
        self.result = content.count('\n')

    def reduce(self, other):
        self.result += other.result


def execute(workers):
    threads = [threading.Thread(target=w.map) for w in workers]
    for thread in threads:
        thread.start()
        thread.join()

    first, rest = workers[0], workers[1:]
    for other in rest:
        first.reduce(other)

    return first.result


def map_reduce(input_class, worker_class, config):
    gen_workers = worker_class.generate_workers(input_class, config)
    workers = list(gen_workers)
    return execute(workers)


if __name__ == '__main__':
    result = map_reduce(FileInputData, LineCountWorker, {'dir_path': 'temp'})
    print(result)

It can be seen that in Python, not only can objects be created through the __init__ constructor, but also objects can be constructed polymorphically through the @classmethod form.

 

Guess you like

Origin blog.csdn.net/zhu6201976/article/details/109316948