python - Access to variables from outside function -


I'm quite new to Python, but I'm a little more experienced in C ++, this is the reason that the code below The example of me a little bit

  def foo (): y = x print yx = 5 foo ()  

The value of this code prints the value 5. How the value variable X fu () is known inside? The above code will not run in C ++, it would be if we did:

  #include & lt; Iostream & gt; Int x = 5; Zero foo () {std :: cout & lt; & Lt; "X =" & lt; & Lt; X & LT; & Lt; Std :: endl; } Int main () {foo (); Return 0; }  

Because here x is a variable in the global variable that has been declared (and defined) before foo (). Is it working in Python because x is added to the global symbol table?

Thanks for any help!

itemprop = "text">

The real distinction is that how the variable lookup works in two languages.

In C ++, the code is compiled in one pass when the compiler reads the code for foo , then it looks at x And it does not know how this identifier can still be a variation of any type, or it can be a function, or it is a typographic The error may occur if it has not already seen any definition for x - something that explains that what type of object is x Though it does not necessarily mean that its real value - it will report immediately an error.

When Python looks at x , then it recognizes that name - that is, an identifier - because it matches the appropriate token type, and There are no language keywords. In Python, everything is an object and we actually mean that - it involves tasks, there is no type of inquiry at compile time, so we do not care that x < / Code> is an integer, or a function, or just what - all these things can be controlled in the same way. (Yes, you can print the function - but it does not show you anything useful as a basic code, it only gives you some basic information about type, name and object ID). There can not really be any type of inquiry, because Python's variables are not of one type (such as type-infrared languages ​​such as Haskell) - a type of value of Python. / P>

Also there is no validity check to see that actually exists, because there can be no again - methods present in Python dynamically create names (please don 't usually Speaking).

However, to determine whether x is not a local variable, there can be sufficient analysis in the compilation of time, therefore the Python code produces "one Global variable name x and use it ". Exception as when foo actually runs , any resultant errors occur if x is actually global If the form does not exist, then it will increase the name error ; If it exists but the related object is wrong type (usually impossible for print , but possibly for + ), it raises TypeError , And if this is the right type but has an invalid value, it will usually either increase the value of ValueError or its subtype (e.g., list contains an invalid numeric Index IndexError , which is a subtype of ValueError ).


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -