0

So I have written a function f(a, b). The structure is:

def f(a, b):
    global result

    def g(a, b):
        global result
        return result

    deg h(a):
        return a  # an updated a

    g(a, b)
    a = h(a)
    g(a, b)
    a = h(a)
    g(a, b)
    a = h(a)
    g(a, b)
    return result
  • You can see that I have two sub-functions g(a, b) and h(a) in f(a, b).
  • I need to run g(a, b) to get the partial result
  • Then I updated the a
  • So that I can run g(a, b) again, to update the result.
  • After 4 runs of g(a, b), I got the full piece of result and return the value.

There must be a way to structure the process here that looks simpler and clearer.

I also tried:

g(a, b)
g(h(a), b)
g(h(h(a), b)
g(h(h(h(a), b)

And that looks hideous as well.

I need some suggestion to simply to structure here, maybe to use the map() or other high order functions? But I just couldn't figure it out.

Thanks!

4
  • map works on lists - it has nothing to do with high-order functions Commented Nov 10, 2017 at 17:09
  • What's the purpose of the global result? And why aren't you saving the value returned by the g function? And why doesn't g do anything with the arguments you pass it? Commented Nov 10, 2017 at 17:09
  • Have you considered using function returns instead of a global variable? Then a simple for loop would let you do it 4 times. Commented Nov 10, 2017 at 17:10
  • @PM2Ring I guess I was not giving enough details. The reason to do global result is because the g(a, b) only returns part of the result. So i need to update the a, and run g() again, the another part of the result. I have to run g() 4 times to get the full result. But again, I am a beginner here, if you can show me how to structure it without using global I will appreciate it. I understand that the use of global is often avoided. Commented Nov 10, 2017 at 18:26

2 Answers 2

2

First off you should use arguments and return values, and not side effects (i.e. returning or modifying global variables). I find that the use of global result makes your code unreadable, but I think you are asking about dynamic function composition, which can be expressed like this:

def compose(*fv):
    return reduce(lambda f, g: lambda x: f(g(x)), fv)

And then used like this (note that the evaluation order is right-to-left, which is common):

def add1(x): return x + 1
def mul2(x): return x * 2

>>> compose(add1, add1, add1)(1)
4
>>> compose(mul2, add1, add1, add1)(1)
8

This is common in functional languages, but I find that when you want this pattern in python, you're not making things simple enough.

Sign up to request clarification or add additional context in comments.

Comments

1

If what you want to do is something like that:

g(a, b)
a = h(a)
g(a, b)
a = h(a)
g(a, b)
a = h(a)
g(a, b)

You can do:

for i in range(4):
    g(a, b)
    a = h(a)
g(a, b)

a bit more compact

1 Comment

range isn't a statement, should have parenthesis.

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.