Hamish
Hamish

Reputation: 503

How can I have a function run within another function, but evaluate from different lists in each function?

I am using one function to do the testing (func1) and the other two functions have different x lists.

What I am attempting to do is have the function refer to different x lists for different functions (func2 and func3). Is there anyway func1 can refer to different x lists within func2 and func3.

def func1(letter):

    if letter in x:
        print True

def func2(letter):
    x = [a,b,c,d,e]

    return func1

def func2(letter):
    x = [e,d,c,b,a]

    return func2

Upvotes: 1

Views: 49

Answers (1)

AntiMatterDynamite
AntiMatterDynamite

Reputation: 1512

you can create the function dynamically when needed:

def make_func1(x):
    def func1(letter):
        if letter in x:
            print True
    return func1

def func2(letter):
    x = [a,b,c,d,e]
    return make_func1(x)

def func3(letter):
    x = [e,d,c,b,a]
    return make_func1(x)

this will create two different func1 functions each with its own x bound to it

Upvotes: 1

Related Questions