-1

I am beginner to python and I am trying to create a Flask application which uses gevent of gunicorn.

I have a function which accepts a number of parameters. say, main_function(A,B,C,D,E,F,G,H,I).

From this function, I have to call another function, function_one(A,B) which calls another function from within, say function_two(A,B) which in turn calls another function function_three(A,B) and so on.

At one point it reaches a function_thirtythree which should get the parameters C,D along with A and B.

Now my question is, do I have to pass C and D to function_one which should then be passed to function_two, then to function_three and so on, only to be used at function_thirty three call ? Is there a better way of doing it ?

def function_thirtythree(A,B)
    return A + B+ C + D

def function_three(A,B)
    function_thirtythree(A,B)

def function_two(A,B):
    function_three(A,B)

def function_one(A,B):
    function_two(A,B)


def main_func(A,B,C,D,E,F,G,H,I):
    function_one(A,B)
davidism
  • 121,510
  • 29
  • 395
  • 339
Anton
  • 3
  • 2

2 Answers2

1

If you are using Flask, you can use the application context to store common request variables globally within a request (see https://flask.palletsprojects.com/en/1.1.x/appcontext/?highlight=context%20global#storing-data).

import flask

def main_function(A, B, C, D, E, F, G):
    flask.g.C = C
    flask.g.D = D

    ...

def function_thirtythree(A, B)
    return A + B + flask.g.C + flask.g.D

This would of course require you to have flask as a dependency of whatever library code function_thirtythree is defined in.

0

This is called "Scope", which determine which variables are available at each point in the code. "Closure" means the nested function can access variables from its parent function. If this example, you will not have to pass x,y,z down:

def outer(x):
    def middle(y):
        middle_scope = 'middle'
        def inner(z):
            return '%s %s %s %s' %  (x, y, middle_scope, z)
        return inner
    return middle

outer('x')('y')('z')

>>> 'x y middle z'

Or:

def outer(x, y, z):
    def middle(y=y):
        middle_scope = 'middle'
        def inner(z=z):
            return '%s %s %s %s' %  (x, y, middle_scope, z)
        return inner
    return middle

outer('x', 'y', 'z')()()

>>> 'x y middle z'

UPDATE:

Just saw the edit to your question. Those functions are not nested. function_thirtythree(A,B) will not have access to variables C or D. Those are outside its scope. You will have to pass them down, or else assign as global variables, like this:

A = 1
B = 2
C = 3
D = 4

def function_thirtythree(A,B)
    return A + B + C + D

function_thirtythree(5,4)

>>> 16

How will you assign C & D. How about this?

def function_thirtythree(A,B)
    C = something
    D = something_else
    return A + B + C + D

Otherwise, go ahead and pass those variables down to the end. Do not worry about passing C, D to functions 2,3,4, etc,. They do not use them, so will not alter them. They will simply pass through:

def function_thirtythree(A,B,C,D)
    return A + B + C + D

def function_three(A,B,C,D)
    #do something with A & B only
    function_thirtythree(A,B,C,D)

def function_two(A,B,C,D):
    #do something with A & B only
    function_three(A,B,C,D)

def function_one(A,B,C,D):
    #do something with A & B only
    function_two(A,B,C,D)


def main_func(A,B,C,D,E,F,G,H,I):
    function_one(A,B,C,D)
GAEfan
  • 11,244
  • 2
  • 17
  • 33
  • Will adding them as global variables create any confusion when I deploy it in a production like scenario when many number of users can concurrently use my product? Suppose the main_func gets called every time an image gets uploaded, will it cause irregular behaviour if many number of users are uploading images at the same time ? – Anton Jul 31 '20 at 17:45
  • That depends on how you assign them. See edit – GAEfan Jul 31 '20 at 17:47
  • If I am modiying the value of C , say C= 8 inside function_thirtythree in your example when UserA uploaded an image; and at the same time UserB uploaded the image, will C be 8 for UserB also, or will it be 3 ? – Anton Jul 31 '20 at 17:51
  • Also, I wont be able to define values of A, B, C, D like this. These values come as an input to main_funct which will vary for each image that is uploaded. My problem is I don't want to pass C and D to function_one and function_two only to be used in a later stage to make a cal to function_thirty_three. I don't want the second image uploaded to use the value which first doc used. – Anton Jul 31 '20 at 17:55
  • See further edit – GAEfan Jul 31 '20 at 17:56