脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - 详解Python为什么不用设计模式

详解Python为什么不用设计模式

2021-12-08 10:21有数可据 Python

今天给大家带来的是关于Python的相关知识,文章围绕着为什么Python不用设计模式展开,文中有非常详细的介绍及代码示例,需要的朋友可以参考下

前言

刚刚看了europython 2017一篇演讲,why you don't need design patterns in python,为什么python不用设计模式。演讲者是stxnext的sebastian buczynski。

他对设计模式的定义是:

  • 常见问题的通用可复用解决方案
  • 定型的最佳实践

他说设计模式是一种似曾相识(anology),是一种大纲(outline),他认为设计模式并不是拿来就能用的。

singleton

详解Python为什么不用设计模式

第一个是singleton模式,singleton的精髓就是任何时候,只有一个类的实例。

《设计模式》里面给出的singleton代码是

声明:

?
1
2
3
4
5
6
7
8
class singleton {
public:
    static singleton* instance();
protected:
    singleton();
private:
    static singleton* _instance;
};

实现:

?
1
singleton* singleton::_instance = 0;

sebastian 在 google 上面找singleton的python实现,找到了以下代码:

声明:

?
1
2
3
4
5
6
class singleton:
    _instance = none
    def __new__(cls, *args, **kwargs):
        if not cls._instance:
            cls._instance = super().__new__(cls, *args, **kwargs)
        return cls._instance

实现:

?
1
2
3
one_instance = singleton()
another_instance = singleton()
one_instance is another_instance # true

sebastian指出,照抄c++,当然也可以解决问题,但是在python里面有更好的解决方案。比如,可以用@classmethod。不过,最好的解决方案是直接用module。因为module本身就是唯一的,相当于module就实现了singleton,那么,我们为什么要大费周章,搞一个singleton出来呢?

我回忆了一下,尽管singleton是最简单的设计模式了,但是,我这么多年一直没用。以前写c#的时候,我用的是静态类,静态类本身就是唯一的,所以我不需要singleton。当然,我看到有人也用c#写了和c++一样的singleton,但是我觉得解决问题就可以了,没必要为了写设计模式而写设计模式。同样,写vb.net的时候,我直接用的module,也不需要singleton。

结论:当年《设计模式》里面的singleton模式,是为了只有一个类实例。如果编程语言本身,如python, c#, vb.net,已经提供了这样的能力,就没有必要再用c++的套路了。或者说,设计模式就不需要了。

facade

详解Python为什么不用设计模式
详解Python为什么不用设计模式

(以上图片来自参考[1])

facade的基本概念是,子系统用facade来屏蔽内部的复杂实现。

这时,我们可以把子系统的python文件统一放在一个文件夹里,然后在这个文件夹里放一个__init__.py文件。

详解Python为什么不用设计模式

command

command模式把请求封装成对象。

sebastian认为,在python里面,函数就是一等公民,所以没有必要创建对象。

?
1
2
def command(discount_rate):
some_obj.notify_users_about_discount()

也可以用functools创建command

?
1
2
3
4
5
6
7
import functools
command = functools.partial(
some_obj.notify_users_about_discount, discount_rate=0.5
)
command()
# equals to
some_obj.notify_users_about_discount(discount_rate=0.5)

visitor

python里面没有接口,没有方法重载。那么怎么实现visitor呢?

sebastian指出,可以用@singledispatch。

?
1
2
3
4
5
from functools import singledispatch
@singledispatch
def visit(node):
    type_name = type(node).__name__
    raise attributeerror(f'no handler found for {type_name}')
?
1
2
3
4
5
6
7
from ast_nodes import assign, functiondef
@visit.register(assign)
def visit(node):
    pass
@visit.register(functiondef)
def visit(node):
    pass

我们看到,这里的实现,并没有class。

decorator

decorator可以用来扩展一个对象。

它实现的方法是新建一个类,这个类和原来的类属于同一个接口。然后这个类接受一个原来的类的对象,每个方法都调用原来的类的方法。

如果套用c++的《设计模式》,我们有

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class originalclass:
    def get_text(self):
        pass
    def get_number(self):
        pass
 
    
class decorator:
    def __init__(self, decorated_obj):
        self.decorated_obj = decorated_obj
    def get_text(self):
        return f'<b>{self.decorated_obj.get_text()}</b>'
    def get_number(self):
        return self.decorated_obj.get_number()

但是,这里可以用python的__getattr__特性来简化实现。

?
1
2
3
4
5
6
7
class decorator:
    def __init__(self, decorated_obj):
        self.decorated_obj = decorated_obj
    def get_text(self):
        return f'{self.decorated_obj.get_text()}'
    def __getattr__(self, attr_name):
        return getattr(self.decorated_obj, attr_name)

总结

sebastian指出,python非常灵活。和25年前的c++大相径庭。很多地方,都非常容易插入逻辑。过去的设计模式,可能并不适用了。我们应该很好的了解python,并借鉴其他语言,而不是生搬硬套。

我觉得,再好的东西,也要和实际相结合。任何脱离实际的做法,都是多余的,甚至有害的。任何理论,方法的产生,都有当时的历史背景,技术背景。如果不了解背后的机制,不了解背后的精神和目的,而是专注于招式本身,那只能是越来越僵化。看似坚持,实际上是背叛。坚持是说固执的坚持原来的做法,背叛是指背叛了初衷。

参考

[1] why you don't need design patterns in python

[2] design patterns – elements of reusable object-oriented software

到此这篇关于详解python为什么不用设计模式的文章就介绍到这了,更多相关python设计模式内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/juwikuang/article/details/117898815

延伸 · 阅读

精彩推荐