简体   繁体   English

在python库中覆盖一个function

[英]Overwrite a function in a python library

I want to overwrite a function in a python library.我想在 python 库中覆盖一个 function。 Example: This is my library, already compiled, in example.py示例:这是我的库,已经编译,在 example.py 中

def my_function():
      return "Hello World"

if __name__ == "__main__":
      return my_function()

Of course if I run example.py it prints "Hello World".当然,如果我运行 example.py,它会打印“Hello World”。

What I want to do is to be able to overwrite my_function() in terms to return another stuff (Example "Hello...") and be able to run example.py and print "Hello..."我想要做的是能够覆盖my_function()以返回另一个东西(例如“Hello ...”)并且能够运行 example.py 并打印“Hello ...”

I need that because I want to deliver a library used by my users in which they can customize some code, like in the example.我需要它,因为我想提供一个供我的用户使用的库,他们可以在其中自定义一些代码,如示例中所示。 How can I do it?我该怎么做?

---EDIT--- The users will not touch the "main" in example.py. ---编辑--- 用户不会触及 example.py 中的“main”。

in the example.py I call my_function() but I want to overwrite my_function() in the example.py in the user code.在 example.py 中我调用了 my_function() 但我想在用户代码的 example.py 中覆盖 my_function() 。

When your users will import the module they will do:当您的用户将导入模块时,他们将执行以下操作:

import the_module

to use your function then they would do使用您的 function 然后他们会做

the_module.my_function()

This is technically possible to do something like这在技术上是可行的

import the_module

def replacement():
    print("something else")

the_module.my_function = replacement

But I don't see any added value here between this and them just creating the function themselves.但我看不到这与他们自己创建 function 之间有任何附加值。

Where it can come with value, it is in OOP, where then you will create a class with a set of given method, and one can inherit your class and override some methods, still keeping others.它可以带来价值的地方是 OOP,然后您将使用一组给定的方法创建一个 class,并且可以继承您的 class 并覆盖一些方法,同时保留其他方法。

class Animal:
    def say(self):
        print("something")
     
    def sleep(self):
        print("sleeping")

class Cat(Animal):
    def say(self):
        print("Miaou")

I'm sure there can be some hack to do this with functions, but better to use Object-Oriented-Programming style to support this functionality in an understandable way.我敢肯定会有一些技巧可以用函数来做到这一点,但最好使用面向对象的编程风格以一种可以理解的方式支持这个功能。 You can define a class containing your functions, and users can override some functions by inheriting this class and implmenting their own variants of the functions:你可以定义一个包含你的函数的 class ,用户可以通过继承这个 class 并实现他们自己的函数变体来覆盖一些函数:

# my_library.py

class LibraryFunctionality:
    def my_function():
        return "My function"

    def my_other_function():
        return "My other function"

then users could do那么用户可以做

# user.py
import my_library

class MyFunctionality(LibraryFunctionality):
    def my_function():
        return "My unique function"

MyFunctionality.my_function()          # returns "My unique function"
MyFunctionality.my_other_function()    # returns "My other function"

If you have a large method, that you want to let users to override by parts (without requiring them to repeat the same implementation), you can use a "Template method" design pattern, where you split a big method into many functions for all the steps, and then users can override some steps selectively.如果你有一个大方法,你想让用户按部分覆盖(不需要他们重复相同的实现),你可以使用“模板方法”设计模式,你将一个大方法分成许多函数供所有步骤,然后用户可以有选择地覆盖某些步骤。 For example something as big as this:例如像这样大的东西:

# my_library.py
class LibraryFunctionality:
    def before_initialization_step():
        pass

    def initialization_step():
        return "My"

    def after_initialization_step(state):
        return state

    def before_work_step(state):
        return state

    def work_step(state):
        return state + " function"

    def after_work_step(state):
        return state

    def before_return_step(state):
        return state

    def return_step(state):
        return state

    def my_function():
        LibraryFunctionality.before_initialization_step()
        state = LibraryFunctionality.initialization_step()
        state = LibraryFunctionality.after_initialization_step(state)

        state = LibraryFunctionality.before_work_step(state)
        state = LibraryFunctionality.work_step(state)
        state = LibraryFunctionality.after_work_step(state)

        state = LibraryFunctionality.before_return_step(state)
        return LibraryFunctionality.return_step(state)

Then users can override specific steps as I've already shown:然后用户可以覆盖我已经展示的特定步骤:

# user.py
import my_library

class MyFunctionality(LibraryFunctionality):
    def before_work_step(state):
        return state + " unique"

LibraryFunctionality.my_function()     # returns "My function"
MyFunctionality.my_function()          # returns "My unique function"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM