[Python] Partial Function Inheritence in Python?

Discussão em 'Python' iniciado por Stack, Junho 19, 2025.

  1. Stack

    Stack Membro Participativo

    I have two files in a library I am creating:

    file1.py

    def func1(a, b):
    c = a + b
    return c


    file2.py

    def func2(a, b):
    c = a - b
    return c


    Now I want each file to have a function that takes the function from that file as an argument, but otherwise does the same thing. So a function like:

    def new_func(f, x, y, z):
    return f(x, y) * z


    Since I don't want to repeat myself, I would prefer to define this function in a separate file, then use some sort of function inheritance that creates a partial function with the first argument set to the func at the beginning of that file. So users can access new_func using file1.new_func(x, y, z) and file2.new_func(x, y, z).

    For context, I have many new_funcs I want to add to about a handful of files. Each would take in the func at the beginning of the file as a fixed argument, so essentially a partial function. But beyond that, they would all do the same thing with that func.

    Is this possible using the functional programming paradigm?

    I have Googled like crazy about function inheritance, partial functions and decorators (as I thought that might help), but have so far found nothing like what I am attempting to do.

    Continue reading...

Compartilhe esta Página