аватар question@mail.ru · 01.01.1970 03:00

Using Global in Python

Hello, I meet Python and after C ++ I categorically do not understand the principle of using Global variables. If you indicate Global in the variable in Def, whether it can be used outside this function, I definitely can’t do it. Maybe just because I’m a beginner and obvious things for me are still too complicated.

   def   global  urokb_in urokb_in =  1    with   open  ( 'users.txt' ,  'r' )  as  f:  line = f.Rreadline ()  f.close ()   of   open  ( 'progress.txt' ,  '` `)  as  k:   While   true :   global  uroka_in lines = k.readline ()   if  line == lines.rsstrip ():  uroka_in =  int  (k.Riredline (). RSTRip ())  urooka_in +  1   uroka_str =  str  (uroka_in)  urokb_str =  str  (urokb_in)  lines.replace (uroka_str, urokb_str)  k.close ()   break    if   not  lines:  k.close ()  urokb_in =  1    with   open  ( 'progress.txt' ,  'a' a ')  as  j: : : :  J.Write (Line)  J.Write ( '\ n' )  J.Write ( str  (urokb_in))  j.Write ( '\ n' )  . J.Close ()   break    if  urokb_in == :  educate__scr = Photoimage (File =  'e_scr_1.png' )  educate_label = label (root, imge = educate__scr)  educate_label.place (x =  0 , y =  0 )     

more experienced encoders tell me how to fix it. Syntaxerror: Name 'Urokb_in' Is used Prior to Global Declaration

аватар answer@mail.ru · 01.01.1970 03:00

If you do not specify Global or Nonlocal, then.

If you want a new global variable create/or reduce the old inside the function (not at the global level), you can use global. This can be useful when working with multiprocessing module in order to inherit the desired global variables from the parental process:

   def   init  ( shared_arr _ ):   global  shared_arr shared_arr = shared_arr_ # must be inherited, not passed as an argument    

. (Otherwise, for example, I would have to declare Global in every function in each function of every global function. Generally , that is, avoid global . Consider each appearance of Global in the code by a mistake if there are no particular reasons for using this design (as in the example with multiprocessing ). If you can’t get rid of Global, then create a minimum code example and ask a separate question: "" How to get rid of Global, making x ""

Latest

Similar