Hello everyone. If \ _ \ _ name \ _ \ _ =='\ _ \ _ main \ _ \ _', who writes the code lazily, raise your hand Has exceeded 100 likes, thank you. Teacher, I have something to tell everyone.
Everyone, I overlook the Shadows name
warning.
Explain the horror of Shadows name
caused by polluting the global scope.
Sorry for the tricky example, but suppose you have the following code:
import sys
def process(n):
x = 0
# ...Various processing
if x > 1:
n *= 10
# ...Various processing
return n
if __name__ == '__main__':
for x in sys.argv[1:] or [1, 2, 3]:
process(int(x))
For some reason, I no longer need to start the process function, so I commented out the variable x
.
However, I overlooked the variable x
that was buried in the code in the middle.
def process(n):
#Comment out here
# x = 0
# ...Various processing
# !!!!I forgot to comment out!!!!
if x > 1:
n *= 10
# ...Various processing
return n
In this case, x
is referenced as a global variable, so no error occurs and it is not detected by static analysis tools.
Simple mistakes in Python can be detected by 99% static analysis, but Shadows name
is a nasty bug that slips through it.
By bypassing static analysis, you need to pay close attention to even one comment out.
** So don't dissipate the Python teacher's language specs. ** **
Of course, the previous example was an extreme situation, but it's quite possible. If you run the risk of causing such a nasty bug, it's easier to wrap the function under ʻif name =='main'` as soon as possible.
In fact, tools like PyCharm that do static analysis in real time will take care of you as you write your code, so this problem may not occur very often.
However, if you ignore the warning or turn it off, this will happen. Silenced PyCharm teacher ...
Python's static analysis tools prevent sometimes confusing bugs. It's important to always keep your code green so that you don't overwhelm the warning.
In the first place, ʻif name =='main'` is the place to write the process you want to execute only in the top level script environment.
So what is a top-level script? ** "Main module" ** that runs directly from Python. Scripts imported from the main module or other scripts are simply called ** "modules" **.
https://docs.python.jp/3/tutorial/modules.html
Scripts that run only as the main module basically don't need ʻif name =='main. Because
name can only contain
main`, it is useless to write an if statement.
** "Then, you don't have to write ʻif name =='main` in the main module" **
It will be.
The teacher's opinion is ** "If you want to write some code in the main module, you should write ʻif name =='main`" **.
For example, if you have the following main module:
import sys
def main(args=sys.argv):
#What a mess
return 0
if __name__ == "__main__":
sys.exit(main())
This is a typical main module process. ʻIf name == The main module without'main` looks like this:
import sys
def main(args=sys.argv):
#What a mess
return 0
sys.exit(main())
The two do the same thing, but the second main module has only one problem.
** Others can't patch monkey **
about it.
Monkey patch is a technique that hijacks the behavior of an existing library and dynamically rewrites it.
[Monkey Patch Wikipedia](https://ja.wikipedia.org/wiki/%E3%83%A2%E3%83%B3%E3%82%AD%E3%83%BC%E3%83%91%E3 % 83% 83% E3% 83% 81)
See the teacher's article for an example of a monkey patch.
I made "sphinx-quickstart-plus" to make Sphinx documentation convenient
In other words, when someone else wants to extend your script, without ʻif name =='main`, the code will be difficult to monkey patch.
Suppose you have a main module called ʻa.py` like this:
a.py
import sys
def main(args=sys.argv):
#What a mess
return 0
sys.exit(main())
If you try to import this ʻa.main` function as a monkey patch (extension), the program will end.
monkey.py
import a
print("monkey patch")
a.main([])
Therefore, it is safer (not required) to write ʻif name =='main in the main module as well. However, ʻif __name__ =='__main__
is unnecessary for sample code and abandoned code.
These are the two things the teacher wants to say.
――Dirty global scope is an act that reduces the reliability of static analysis, Guilty! --It is safer to write ʻif name =='main` in the main module as well.
that's all.
Recommended Posts