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

Why is the global variable inaccessible from the function?

Please tell me why the function does not display the value of s

  S =  "" I'M Global Variable ""    def   foo  ():   print  (s) r =  '' reset global Variable ' foo ()     

I assumed that when the interpreter reaches the Print (S) line, it will understand that in the field of visibility of the FOO function there is no such variable. Therefore, it will begin to look for it in the exteal area of ​​visibility, but there is it.

However, as a result, the following error message is displayed in the console:

  Traceback (Most Recent Call Last):  File , line  7 ,  in  & lt; moodule & gt;  foo ()  file  "" index.py "" , line  4 ,  in  foo  print  (s) unboundLoCaleror: Local Variable  'S'  refereNCED Before Assignment    

The problem disappears if the line

  S =  'Reset Global Variable'      

But I still do not understand the mechanism

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

Mark Lutz - “Study Python”, Chapter 20, Page 596

as you know, the names that are performed inside the function are considered as local & ndash; They are located in the field of visibility of the function and exist only during the function of the function. But I have not yet said that local variables are determined statically, during the compilation of the software code in the DEF instructions, and not in accordance with the appropriation operations performed during execution. This feature causes the appearance of the most bizarre messages in the news group received from novice programmers. Usually, if any value is not assigned inside the function, its search will be carried out in the field of visibility of the overtuing module. But look at what will happen if you add the instructions for assigning a variable X after its use.

  x =  99    & gt; & gt; & gt;   def   Selector  ():   ...   print  (x) #"># " exists!    ...  x =  88  # x is classified as a local variable    ...  # ">#" The most happens with & ldquo; Import X & RDQUO;, & LDQUO; Def X & RDQUO; ...    & gt; & gt; & gt;  Selector () traceback (Most Recent Call Last):  ... The text of the error message is omitted ... unboundLocaleror: Local Variable & Lsquo; X & Rsquo; Referenced Before Assignment    

a message was received that the variable was not determined, but the reason for its appearance is not obvious. This program code is compiled by the interpreter during entry into an interactive shell or during the import of the module. In time, the Python compilation reveals the operation of assigning a variable X and concludes that x & ndash; This is a local name everywhere in the body of functions. But during the performance of the function, due to the fact that by the time the Print instructions were not carried out, the assignment operation was not yet carried out, the interpreter reports that the name is not determined. According to this rule of use of names, he says that the appeal to the local variable X was made before it was assigned a value. In fact, any operation of assignment inside the function creates a local name. Importing operation, =, invested Def instructions, invested class definitions and so on & ndash; All are interpreted in this way. The problem arises due to the fact that the assignment operation makes names local for the entire function, and not just for the part that follows the instructions of assignment.

Read textbooks is useful.

.

Latest

Similar