Search code examples
pythonpython-3.xdesign-patternspython-objectobject-notation

How is this Python design pattern called?


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 ?


Solution

  • 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.

    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