Scripting inside a Python application

前端 未结 4 1940
清歌不尽
清歌不尽 2021-01-19 00:50


I\'d like to include Python scripting in one of my applications, that is written in Python itself.

My application must be able to call extern

4条回答
  •  时光取名叫无心
    2021-01-19 01:05

    "My application must be able to call external Python functions (written by the user) as callbacks".

    There's an alternative that's often simpler.

    Define classes which call method functions at specific points. You provide a default implementation.

    Your user can then extended the classes and provide appropriate method functions instead of callbacks.

    This rarely requires global variables. It's also simpler to implement because your user does something like the following

    import core_classes
    class MyExtension( core_classes.SomeClass ):
        def event_1( self, args ):
            # override the default behavior for event_1.
    
    core_classes.main( MyExtension )
    

    This works very smoothly, and allows maximum flexibility. Errors will always be in their code, since their code is the "main" module.

提交回复
热议问题