Question

Yes, I want to generate functions using functions.

I have hit a wall at how to cleanly get function1 to pass arguments into a function2 (which is defined in function1).

def function1(*args):
    def function2(*args):
        print "I will do some stuff or whatever"

    return function2

new_function = function1()

new_function()
------->I will do some stuff or whatever

Anyway, I can't wrap my head around how I can pass new arguments into function2 and then consequently new_function by using function1.

In short, I want to be able to do this:

new_function = function1(arg1, arg2, arg3)

And have arg1, arg2, and arg3 pass into new_function but I can't get the structure right.

Was it helpful?

Solution

Give function1's parameters different names, and then just use them inside the function2 definition:

def division_factory(quotient):
    def divide(divisor):
        return divmod(quotient, divisor)
    return divide

divide_81_by = division_factory(81)
divide_81_by(3) # (27, 0)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top