0

My code is as below:

f=0

def load(request):

    global f
    if f == 0:
        login
        f=f+1
    else:
        some commands

the function load is called several times.so every time the function is called value of 'f' will be set to zero?

zinc
  • 81
  • 1
  • 7
  • no it will remain to the previous value... – Jean-François Fabre Apr 04 '17 at 13:01
  • yes it should remain to previous value but its changing sometime – zinc Apr 04 '17 at 13:02
  • Is this a django view function? Then yes, it will reset after some time – dentemm Apr 04 '17 at 13:09
  • a view can live in different threads so global variables in django views will not behave as expected. – dentemm Apr 04 '17 at 13:16
  • It can also be that OP uses the built-in server that reload on code change and so resets f to 0. Django has much better way to handle access and redirect anonymous users to login, if that's the goal. – polku Apr 04 '17 at 13:21
  • yes @dentemm this is a view function..i din get your second answer..can you tell in detail? – zinc Apr 05 '17 at 05:33
  • @zinc I do not have the technical details myself, but I have run into the same problem for one of my Django websites. Afterwards, I found out that views are generally not thread-safe, see [here](http://stackoverflow.com/questions/2680902/python-django-global-variables) for more information – dentemm Apr 05 '17 at 07:32

0 Answers0