这个Python设计模式如何称呼? [英] How is this Python design pattern called?

查看:54
本文介绍了这个Python设计模式如何称呼?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ffmpeg-python文档中,他们使用以下设计模式作为示例:

In the ffmpeg-python docs, they used following design pattern for their examples:

(
    ffmpeg
    .input('dummy.mp4')
    .filter('fps', fps=25, round='up')
    .output('dummy2.mp4')
    .run()
)

该设计模式如何称呼,我在哪里可以找到更多信息,它的优缺点是什么?

How is this design pattern called, where can I find more information about it, and what are the pros and cons of it ?

推荐答案

此设计模式称为构建器",您可以在

This design pattern called builder, you can read about it in here

基本上,所有命令(运行除外)都会更改对象,然后自行返回,这使您可以构建"目录.物体继续前进.

basically, all the command (except run) change the object, and return it self, which allow you to "build" the object as it go on.

我认为这是非常有用的事情,在查询构建中超级好,并且可以简化代码.

in my opinion is very usefull things, in query building is super good, and can be simplify a code.

考虑要构建的数据库查询,可以说我们使用 sql .

think about db query you want to build, lets say we use sql.

# lets say we implement a builder called query

class query:
    def __init__():
        ...
    def from(self, db_name):
        self.db_name = db_name
        return self
    ....
 
q = query()
    .from("db_name") # Notice every line change something (like here change query.db_name to "db_name"
    .fields("username")
    .where(id=2)
    .execute() # this line will run the query on the server and return the output

这篇关于这个Python设计模式如何称呼?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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