python - Calling locals() in a function not intuitive? -


This can be primary, but can help me understand the namespace. What is a good explanation when the function definition is executed, and then what happens later when the function object is executed. Can recursive things recursive.

The results are not clear at all; I expected:

Local people will include var; Local people will be Y and local people; And locals will include var, locals, and local_2. # function calls to local people (many times), and gives returns to them ... def func (): var = 'Var!' Locals_1 = local (local) local = local (local) local = local () return locales, local locales, locals # # fax is called ... locals, local, localities = fenc () # display results ... print 'Locals:', Localities Number of locals: For local people, number of locals, number of locals, number of locals, number of locals: ',' Locals ': {...},' Local Local ': { ...}} Local_2: {'var': 'var! ',' Localals_1 ': {...},' local force ': {. ..}} locals_3: {'var': 'var!', 'Locals_1': {...}, 'locals_2': {...}}

looks pattern To call local people , all the documents for the returned locals are the same, and they all include the first (N-1) Local-Dictus.

Can anyone explain this?

More specifically:

Why are the locals involved?

Why are Local Local Local_2 included? What is assigned to local people when joke is created, or executed?

And why do the locals do not get involved anywhere?

What "{...}" 'Endless Recycling'? Like photos of the mirrors facing each other?

Let's run this code:

  def func (): Var = 'var!' Local (local) (local) (local) (local) (local) (local) (local) (local) (local) (local) (local) (local) (local) (local) (local) (local) (local) (local) (local) ( Local people (local), locals, locals, funnels ()  

This will be in output:

print (ID (local public), ID (local ()) , Local people ()
  44860744 44860744 {'locales': {...}, 'var': 'var!'} 44860744 44860744 {'local_2': {...}, 'localals_1': {...}, 'Var': 'var!'} 44860744 44860744 {'People L_2 ': {...},' locals_3 ': {...},' localals_1 ': {...},' var ':' var! '}  

< Code> local () increases here as you expect, however refer to locals () , value of each variable local () not

of local () changes after each assignment, but no reference , therefore Each variable is pointing to the same object. All the objects in my output are id s equals The Ut. ).

  local local local_2 local force \ | / \ | / VVV --------------------------------------------- | Single Local () Object ---------------------------------------------  < / Pre> 

They do not know exactly what the value is local () , they only know that when it is necessary (where the variable is used anywhere) . Changes on local () do not affect those variables.

In your work you are returning three variables, and this happens when you are printing them:

  Print (local_en) - & gt; 1. Obtain the object referenced in locals_N 2. Return the value of that object  

View? Therefore, this is the reason that they have the same value, there is a moment of local in

change (in some way) again and then run a print statement, then what will be printed 3 times? Yes, new value of local (new) .


Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -