For example: deffoo (): a10deffoo2 (): a20 for example:
def foo(): a = 10 def foo2(): a = 20
Reply: Isn't this what Wang Yin said? Wang Yin specializes in programming languages. The analysis by doctoral students studying languages on various language features has inspired us a lot. But what you said is that there is a outside, and there is another a in the function. I would like to ask, in fact, in actual development, is it necessary to deal with internal and external layer variable name conflicts every day? We have two options:
One way is to differentiate variables and assign values, which is similar to adding a var before declaration,
In addition, the common practice is as follows: Without var, you only need to add some global nolocal keywords when there is a conflict.
Which method is better? Think about it. In most cases, you won't be stupid enough to use the same name inside and outside of 99%, good Programming habits, even global variables and local variables are different in naming, and there will be no conflict as you said. System design requires constant selection. Many turtles make the same choice, so that you do not need to write a var statement for each variable in 99% of the cases, this ensures the simplicity and elegance of the language. What are the difficulties of using nolocal global when you encounter 1% extreme situations?
However, these elegant features for users are nothing more than a torment for Wang Yin, who writes PySonar. It is painful to point out their differences in static analysis, this is a positive thing. The more advanced the language is, the more difficult it is to perform static analysis. Therefore, Wang Yin is disgusted and can be understood by the violent global nolocal injection.
Therefore, let alone the cloud. Think about the background in which these words are said. These objective conditions are the same as those of your own situation, so you can make your own judgments.
------------------ Add one that is not mentioned at present.
Whether it is a Python design defect or not, the conclusion you have drawn from the above example is biased. In fact, in Python, The cognominal variable in the subfunction cannot change the peripheral cognominal variable without being declared. However, this widely circulated online statement is incorrect.
Several lines of code can illustrate this problem:
Def foo (): a_int = 10 a_list_with_method = [0, 1, 10] def foo2 (): a_int = 20 a_list_with_method.append (11) foo2 () print (a_int) # Use print a_int print (a_list_with_method) for Python2.x # Use print a_list_with_methodfoo () for Python2.x ()
First of all, Python can be said to be "no variable" without completely accurate requirements. Some are "name binding". Each name can be considered as a reference to an instance, variables are just a concept that people are more familiar. That is to say, in the Code 'a = 20', 'A' is just a name (reference ).
Then, return to the subject's code. This is a problem with the Python name search mechanism, because if Python 2.x is implemented and cannot be found locally, it will be searched globally, then the question "there is no way to change the value of a in foo in foo2." is solved by introducing 'nonlocal' in Python3.x.
Finally, "Does python distinguish between variable definition and assignment? Is it a design defect ?", This is a pseudo-problem. I mentioned at the beginning that Python can be called "No variable". Therefore, when it comes to "Definition" and "defect", it is even more a penalty. The closure Design of python is indeed flawed. It is not just this place, but there are reference passing parameters, but there is no force read-only.
However, I told you that this is a feature, not a defect. The design philosophy of python does not encourage you to use the fancy Syntax of lexical scopes. According to the logic of python, you should honestly use the independent variables to import all the parameters. Nonlocal and global are backdoors and are not recommended.
The so-called python design philosophy is simple and direct, follow the feeling, do not compromise the hardware, nor bend to the mathematics. Scope is a complicated mathematical problem. It is wise to bypass it and not to think about it. What is the difference between writing python and Writing C ++? Of course, if you must think about this problem, C ++ welcomes you (it cannot be a defect to escape this... there should be other ways to implement this function. For some languages, such as C, functions cannot be nested.
I think Python does not distinguish between declarations and values. In some cases, if the variable name is misspelled and the runtime may not report errors, various magical bugs may occur. In python, everything is an object, and the value assignment is actually a reference binding, so this situation is inevitable in a sense.
This is also related to the basic principles that python follows. If we follow the simple and unique expression principle, there is no inconvenience. If we say this principle is not good, there is no way ......
In foo2, there is no way to change the value of a in foo. Can it be said that python does not distinguish between variable definition and value assignment? Does the nonlocal introduced by python3 prove this exactly?
What I want to say is that you have drawn a correct conclusion from a wrong argument ..
Python's name rebinding (assign to) mechanism is indeed flawed. It can only rebind to local and module-global, rather than rebind to outer scope.
However, Python declares variables. For example, the global statement is used to bind global variables, while the local statement is implicit. The nonlocal added in Python 3 fixes the issue that cannot be bound to the outer scope.
PEP 3104 -- Access to Names in Outer Scopes
Are you sure you are a design defect, not a virtue )? I think it should be a defect. Consider the following situation:
Variable = 10
Bla...
Varable = 3 // here, due to spelling, I is played less and considered as a new variable.
Variable + = 16
Python is a beginner in computing errors. I have several years of c ++ experience. I 'd like to say a few words that I understand.
For any variable, he should follow the minimum scope. Since Python creates a variable without assigning values separately, let's create it! I don't care about anything else. You want to use other scopes. Okay! You can specify it!
As for why the assignment is created, it may be because Python does not require the declaration of the data type! For optimization, the default reference format is used. It is just an optimization of memory performance!