1

I know the title is very confusing but essentially here is what I am trying to do:

def foo():
    bar(name)

def evaluate_foo(name_table):
    def bar(name, lookup_table=name_table):
        print(name + "'s last name is " + lookup_table[name])
    foo()

Basically, if I know that foo is going to make a call to bar, can I replace bar with my own function right before calling foo?

I am trying to provide a lookup table to bar, but the lookup table is generated inside of evaluate_foo. It's also important to me that someone using the function bar doesn't need to know about the lookup table that is being used by bar within it.

4
  • 1
    bar is local to evaluate_foo. foo cannot see it. What are you trying to accomplish? Commented Jun 27, 2017 at 22:31
  • Could you please make it more clear what it is you're trying to do. p.s. You can't access bar from foo Commented Jun 27, 2017 at 22:31
  • Python has lexical scoping. It sounds like you want dynamic scoping for this to work. Commented Jun 27, 2017 at 22:35
  • @Coldspeed I have updated the example to be a bit clearer about what I am trying to do. Commented Jun 28, 2017 at 7:14

3 Answers 3

1

If I understand your question correctly, you can pass your function to foo to be executed. For instance you could do:

def foo(f):
    f()

def evaluate_foo(some_param):   
    baz = some_param.get_special_number()
    def bar(bar_param=baz):
        print("Bar's result is " + str(baz))
    foo(bar)
Sign up to request clarification or add additional context in comments.

Comments

1

This won't work because foo() will lookup the function referred to by bar in the namespace it's defined in, not the namespace it's called in.

This sort of situation is what object inheritance is for, instead of having function foo() you should redesign it to be object whatever with method whatever.foo() and whatever.bar(). Then you can subclass whatever and write a new method bar() that foo() will find because it's defined in the class namespace.

Presumably you want to do this because you don't control foo() and so you can't convert it from a function into a method. In that case you can achieve this by monkey-patching bar() in the same namespace where foo() is defined, but that's some deep dark voodoo that is generally discouraged.

Comments

1

If I understand you correctly, you want to override a function definition globally. The following is not pretty, but it works (with some caveats below):

def foo():
    print("foo")

def baz():
    print("baz")
    foo()

def bar():
    def local_foo():
        print("local foo")

    global foo
    foo = local_foo

    baz()

if __name__ == "__main__":
    bar()

The output is

baz
local foo

So, the caveats:

You can only do this for global functions, unless you want to jump through even more hoops.

The second point is that, unless you're just doing this for fun or are desperate, you should use other methods (as described by others) to achieve the same effect. E.g. some kind of design pattern.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.