用于预定义类创建的元类与继承 [英] Metaclass vs inheritance for predefined class creation

查看:55
本文介绍了用于预定义类创建的元类与继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个程序编写一些代码,该程序将能够运行一些软件、读取输入/输出并在此过程中进行一些数据处理.例如:(这不是真实情况,只是提供一个想法)

I'm writing some code for a program that will be able to run some software, read the inputs/outputs and do some data processing along the way. For example: (this is not the real case, but just to give an idea)

class MusicFile(object):
   extension = [""]

   def setup(self):
       #set up the data

   def runsoftware(self):
       #play the song

class MP3(MusicFile):
   extension = [".mp3"]

   def setup(self):
       #setupMP3file

   def runsoftware(self):
       #runMP3software

我有大约 4 个通用类,然后文件扩展名将被定义和区别对待.每个类都有其工作所需的定义数量的方法.目前,只支持一两个文件扩展名,但我想留下一个结构,在其中添加更多扩展名很容易(也许是直观的)供其他人完成.

I have about 4 general classes, and then file extensions will be defined and treated differently. Each class has a defined number of methods that are required for it to work. For now, only one or two file extensions will be supported, but I want to leave a structure done where adding more extensions is easy (perhaps intuitive) for others to do.

我编写代码的时间不长,我接到了这个任务,我想以最好的方式做这件事.

I've been coding for not so long and I was given this task, which I want to do the best possible way.

元类的使用是否适合这种情况?为了设置一些必须包含我之前定义的方法的严格类,这样所有方法都将是统一的.我应该坚持简单的继承吗?

Is the use of metaclasses appropriate for this case? In order to set up some strict classes that must contain the methods I have previously defined, that way it will be uniform across all of them. Should I stick to just simple inheritance?

我希望能够注册 MusicFile 的所有子类,以便能够实例化正确的子类(例如,给定文件路径).

I want to be able to maybe have all the subclasses of MusicFile registered to be able to instantiate the right one (given a file path, for example).

我已经阅读过元类,它们似乎很适合这种情况,但由于我也读过,如果您不太了解它们,则不需要使用它们,我想问一下征求您的专家意见.

I have read about metaclasses, and they seem like a good fit for this case, but since I've read also that they don't need to be used if you don't know them very well, I want to ask for your expert opinion.

推荐答案

此答案仅适用于 Python 3.6+.如果您可以选择升级到最新的 Python 版本,您应该有很多理由 以下可能是其中之一.

This answer only applies for Python 3.6+. If you have the option to upgrade to the most recent Python version, there are numerous reasons why you should and the following may be one of them.

Python 3.6 引入了 __init_subclass__ 钩子,它在类被子类化后执行.

Python 3.6 introduced the __init_subclass__ hook that is exectued after a class is subclassed.

class MusicFile:
    _register = {}

    def __init_subclass__(cls, **kwargs):
        if not hasattr(cls, 'extension'):
            raise ValueError(cls.__name__ + ' has no extension')

        MusicFile._register.update({e: cls for e in cls.extension})

    @classmethod
    def get_class_by_extension(cls, ext):
        return cls._register.get(ext)

示例

class MP3(MusicFile):
    extension = ['mp3']

class MIDI(MusicFile):
    extension = ['midi']

MusicFile.get_class_by_extension('mp3') # <class '__main__.MP3'>
MusicFile.get_class_by_extension('midi') # <class '__main__.MIDI'>

请注意,这与 bipll 建议的工厂方法 非常相似,但以更直接和可维护的方式.

Note that this is very similar to the factory approach suggested by bipll, but in a more straightforward and maintainable way.

这篇关于用于预定义类创建的元类与继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆