Name Meaning In Python
As it is an in built variable in python language we can write a program just to see the value of this variable as below.
Name meaning in python. However before doing that it will define a few special variables. Python is such a popular language because it s easy to use and it s comprehensive. Python is such a popular language because it s easy to use and it s comprehensive.
When a python interpreter reads a python file it first sets a few special variables. A special variable called name provides the functionality of the main function. Since there is no main function in python when the command to run a python program is given to the interpreter the code that is at level 0 indentation is to be executed.
If you follow this article step by step and read its code snippets you will learn how to use if name main and why it s so important. Previously i covered what the keyword yield was useful for and with some great feedback i have decided to tackle another key feature. To fully understand what name is and how it used let s go through a series of examples.
Let s create a simple python script in a file called test py test py print name in test py is set to name what this line does is that it is going to print out the variable name on the screen so that we can examine it. Before executing code python interpreter reads source file and define few special variables global variables. Main in this article i want to show you how you can make use of this variable to create modules in python.
If name main. It gets its value depending on how we execute the containing script. Name is one such special variable.
Print type name print name. Every module in python has a special attribute called name the value of name attribute is set to main when module run as main program otherwise the value of name is set to contain the name of the module. If the python interpreter is running that module the source file as the main program it sets the special name variable to have a value main if this file is being imported from another module name will be set to the module s name.