Python中的构造函数和方法有什么不同,我可以将它们混合在一起吗?(What's the different between a constructor & method in Pytho

编程入门 行业动态 更新时间:2024-10-27 09:33:57
Python中的构造函数和方法有什么不同,我可以将它们混合在一起吗?(What's the different between a constructor & method in Python, can I mix them together?)

我实现了一个可插拔的框架,如下所示。

它有效,但我对Plugin2的实现感到困惑。 似乎它使用普通方法而不是类构造函数。

我可以这样做吗? 有没有缺点?

这是一种正常使用的“模式”吗? 如果是,那么模式名称是什么? '打字'? 我应该避免这样使用吗?

更新:我关注的是以下方法:

def addPlugin(name, plugin)

现在参数插件可以是Class或Method。 然后它对插件开发者来说有点模糊。 这是动态编程语言世界的常见情况吗?

class MyFramework(object): _plugins = {} _osType = None @staticmethod def addPlugin(name, plugin): MyFramework._plugins[name]= plugin def __init__(self, osType): self._osType = osType for name, plugin in MyFramework._plugins.items(): setattr(self, name, plugin(self, self._osType)) class Plugin1(object): def __init__(self, owner, osType): self.owner= owner self.osType = osType def hello(self): print 'this is plugin1' def Plugin2(owner, osType): if (osType == "Linux"): return Plugin2Linux(owner) else: return Plugin2Common(owner) class Plugin2Linux(object): def __init__(self, owner): self.owner= owner def hello(self): print 'this is plugin2 Linux version' class Plugin2Common(object): def __init__(self, owner): self.owner= owner def hello(self): print 'this is plugin2 common version' MyFramework.addPlugin("plugin1", Plugin1) MyFramework.addPlugin("plugin2", Plugin2) framework = MyFramework("Linux") plugin1 = getattr(framework, "plugin1") plugin2 = getattr(framework, "plugin2") plugin1.hello() plugin2.hello()

I implemented a plugable framework like below.

It works but I'm confused about the implementation of Plugin2. Seems it uses a normal method instead of a class constructor.

Can I do so? Is there any cons?

Is it a normal-used 'pattern'? If it is, what's the pattern name? 'Duck typing'? Should I avoid usage like this?

UPDATE: My concern is for below method:

def addPlugin(name, plugin)

Now parameter plugin could be either a Class or a Method. Then it is kind of obscure for the plugin developer. Is this a common case in dynamic programming language world?

class MyFramework(object): _plugins = {} _osType = None @staticmethod def addPlugin(name, plugin): MyFramework._plugins[name]= plugin def __init__(self, osType): self._osType = osType for name, plugin in MyFramework._plugins.items(): setattr(self, name, plugin(self, self._osType)) class Plugin1(object): def __init__(self, owner, osType): self.owner= owner self.osType = osType def hello(self): print 'this is plugin1' def Plugin2(owner, osType): if (osType == "Linux"): return Plugin2Linux(owner) else: return Plugin2Common(owner) class Plugin2Linux(object): def __init__(self, owner): self.owner= owner def hello(self): print 'this is plugin2 Linux version' class Plugin2Common(object): def __init__(self, owner): self.owner= owner def hello(self): print 'this is plugin2 common version' MyFramework.addPlugin("plugin1", Plugin1) MyFramework.addPlugin("plugin2", Plugin2) framework = MyFramework("Linux") plugin1 = getattr(framework, "plugin1") plugin2 = getattr(framework, "plugin2") plugin1.hello() plugin2.hello()

最满意答案

这是一种常用的模式,称为工厂功能 。 将接口打开到任何返回对象的callable是一个好主意,无论callable是函数还是类。 它可以让您灵活处理将来想做的事情。

This is an often-used pattern called a factory function. It's a good idea to have your interface open to any callable that returns an object, whether the callable is a function or a class. It keeps you flexible for whatever you want to do in the future.

更多推荐

本文发布于:2023-08-05 15:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1434107.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有什么不同   函数   方法   Python   method

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!