there is a service (at flask), it accepts customer requests, there is a chain of functions that works with user variables. I need that in each they would be available without transmission in the parameters.

approximate structure of the application:

1. ls = "!" 2. @app.route('/<tx>') 3. def get_phrase(tx):#точка входа 4. ls = request.cookies.get('ls') 5. if True: 6. return get_info() 7. def get_info(): 8. return something_func(ls) 

line numbering for explanation.

if I write return ls between line 3 and line 4, I get the error UnboundLocalError: local variable 'ls' referenced before assignment (that is, can I not get the current value at any time?).

if I write after 4, the value obtained in line 4 will be returned regularly.

if return ls instead of 4 lines, then “!” is returned, i.e. global value.

but I need this value in all functions, i.e. in the example, I expect that by writing return ls between 7 and 8 lines I will get the value from the 4th line, however, the “!” is returned, that is, in the higher-level functions, the variable becomes local. How to change its value while preserving its globality?

  • five
    ls on the 4th line is not a global, but a local variable. To make it global, add global ls after 3 lines - andreymal
  • Indeed, thank you, that solved the problem. but about UnboundLocalError: local variable 'ls' referenced before assignment is still not clear, why if I continue on the code as if I change the variable, then I can not get its value before these changes? - Maximmka
  • one
    Make a comment as an answer, I will accept it. and regarding the error I found the answer in ru.stackoverflow.com/questions/358/… - Maximmka
  • What kind of game do you have if True: :? : D - gil9red
  • there was a complicated condition, it was purely for example changed, and only then I thought that it could in principle be removed) - Maximmka

0