Skip to content

Commit 6024a53

Browse files
committed
__name__ to README.md and webpage index
1 parent 9ff417a commit 6024a53

File tree

5 files changed

+127
-19
lines changed

5 files changed

+127
-19
lines changed

README.md

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ All contributions are welcome:
192192
- [Context Manager](#context-manager)
193193
- [with statement](#with-statement)
194194
- [Writing your own contextmanager using generator syntax](#writing-your-own-contextmanager-using-generator-syntax)
195+
- [__main__ Top-level script environment](#main-top-level-script-environment)
196+
- [Advantages](#advantages)
195197
- [Virtual Environment](#virtual-environment)
196198
- [virtualenv](#virtualenv)
197199
- [pipenv](#pipenv)
@@ -214,8 +216,6 @@ From **Highest** to **Lowest** precedence:
214216

215217
Examples of expressions in the interactive shell:
216218

217-
218-
219219
```python
220220
>>> 2 + 3 * 6
221221
20
@@ -4222,9 +4222,9 @@ else:
42224222

42234223
The names ```args and kwargs``` are arbitrary - the important thing are the ```*``` and ```**``` operators. They can mean:
42244224

4225-
1. In a function declaration, ```*``` means “pack all remaining positional arguments into a tuple named <name>”, while ```**``` is the same for keyword arguments (except it uses a dictionary, not a tuple).
4225+
1. In a function declaration, ```*``` means “pack all remaining positional arguments into a tuple named `<name>`”, while ```**``` is the same for keyword arguments (except it uses a dictionary, not a tuple).
42264226

4227-
2. In a function call, ```*``` means “unpack tuple or list named <name> to positional arguments at this position”, while ```**``` is the same for keyword arguments.
4227+
2. In a function call, ```*``` means “unpack tuple or list named `<name>` to positional arguments at this position”, while ```**``` is the same for keyword arguments.
42284228

42294229
For example you can make a function that you can use to call any other function, no matter what parameters it has:
42304230

@@ -4361,6 +4361,49 @@ Exit
43614361

43624362
[*Return to the Top*](#python-cheatsheet)
43634363

4364+
## __main__ Top-level script environment
4365+
4366+
`__main__` is the name of the scope in which top-level code executes.
4367+
A module’s __name__ is set equal to `__main__` when read from standard input, a script, or from an interactive prompt.
4368+
4369+
A module can discover whether or not it is running in the main scope by checking its own `__name__`, which allows a common idiom for conditionally executing code in a module when it is run as a script or with `python -m` but not when it is imported:
4370+
4371+
```python
4372+
>>> if __name__ == "__main__":
4373+
... # execute only if run as a script
4374+
... main()
4375+
```
4376+
4377+
For a package, the same effect can be achieved by including a __main__.py module, the contents of which will be executed when the module is run with -m
4378+
4379+
For example we are developing script which is designed to be used as module, we should do:
4380+
4381+
```python
4382+
>>> # Python program to execute function directly
4383+
>>> def add(a, b):
4384+
... return a+b
4385+
...
4386+
>>> add(10, 20) # we can test it by calling the function save it as calculate.py
4387+
30
4388+
>>> # Now if we want to use that module by importing we have to comment out our call,
4389+
>>> # Instead we can write like this in calculate.py
4390+
>>> if __name__ == "__main__":
4391+
... add(3, 5)
4392+
...
4393+
>>> import calculate
4394+
>>> calculate.add(3, 5)
4395+
8
4396+
```
4397+
4398+
### Advantages
4399+
4400+
1. Every Python module has it’s `__name__` defined and if this is `__main__`, it implies that the module is being run standalone by the user and we can do corresponding appropriate actions.
4401+
2. If you import this script as a module in another script, the __name__ is set to the name of the script/module.
4402+
3. Python files can act as either reusable modules, or as standalone programs.
4403+
4. if `__name__ == “main”:` is used to execute some code only if the file was run directly, and not imported.
4404+
4405+
[*Return to the Top*](#python-cheatsheet)
4406+
43644407
## Virtual Environment
43654408

43664409
The use of a Virtual Environment is to test python code in encapsulated environments and to also avoid filling the base Python installation with libraries we might use for only one project.

blog_files/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@
1818
- [Ternary Conditional Operator](#ternary-conditional-operator)
1919
- [args and kwargs](#args-and-kwargs)
2020
- [Context Manager](#context-manager)
21+
- [__main__ Top-level script environment](#main-top-level-script-environment)
2122
- [Virtual Environment](#virtual-environment)

blog_files/pysheet.md

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3731,9 +3731,9 @@ else:
37313731

37323732
The names ```args and kwargs``` are arbitrary - the important thing are the ```*``` and ```**``` operators. They can mean:
37333733

3734-
1. In a function declaration, ```*``` means “pack all remaining positional arguments into a tuple named <name>”, while ```**``` is the same for keyword arguments (except it uses a dictionary, not a tuple).
3734+
1. In a function declaration, ```*``` means “pack all remaining positional arguments into a tuple named `<name>`”, while ```**``` is the same for keyword arguments (except it uses a dictionary, not a tuple).
37353735

3736-
2. In a function call, ```*``` means “unpack tuple or list named <name> to positional arguments at this position”, while ```**``` is the same for keyword arguments.
3736+
2. In a function call, ```*``` means “unpack tuple or list named `<name>` to positional arguments at this position”, while ```**``` is the same for keyword arguments.
37373737

37383738
For example you can make a function that you can use to call any other function, no matter what parameters it has:
37393739

@@ -3863,23 +3863,22 @@ It is also possible to write a context manager using generator syntax thanks to
38633863
Enter
38643864
Right in the middle with cm = 3
38653865
Exit
3866-
38673866
>>>
3868-
38693867
```
38703868

38713869
## __main__ Top-level script environment
38723870

3873-
```__main__``` is the name of the scope in which top-level code executes.
3874-
A module’s __name__ is set equal to ```__main__``` when read from standard input, a script, or from an interactive prompt.
3871+
`__main__` is the name of the scope in which top-level code executes.
3872+
A module’s __name__ is set equal to `__main__` when read from standard input, a script, or from an interactive prompt.
38753873

3876-
A module can discover whether or not it is running in the main scope by checking its own ```__name__```, which allows a common idiom for conditionally executing code in a module when it is run as a script or with ```python -m``` but not when it is imported:
3874+
A module can discover whether or not it is running in the main scope by checking its own `__name__`, which allows a common idiom for conditionally executing code in a module when it is run as a script or with `python -m` but not when it is imported:
38773875

38783876
```python
38793877
>>> if __name__ == "__main__":
38803878
... # execute only if run as a script
38813879
... main()
38823880
```
3881+
38833882
For a package, the same effect can be achieved by including a __main__.py module, the contents of which will be executed when the module is run with -m
38843883

38853884
For example we are developing script which is designed to be used as module, we should do:
@@ -3891,7 +3890,7 @@ For example we are developing script which is designed to be used as module, we
38913890
...
38923891
>>> add(10, 20) # we can test it by calling the function save it as calculate.py
38933892
30
3894-
>>> # Now if we want to use that module by importing we have to comment out our call,
3893+
>>> # Now if we want to use that module by importing we have to comment out our call,
38953894
>>> # Instead we can write like this in calculate.py
38963895
>>> if __name__ == "__main__":
38973896
... add(3, 5)
@@ -3901,12 +3900,12 @@ For example we are developing script which is designed to be used as module, we
39013900
8
39023901
```
39033902

3904-
### Advantages:
3905-
1. Every Python module has it’s ```__name__``` defined and if this is ```__main__```, it implies that the module is being run standalone by the user and we can do corresponding appropriate actions.
3903+
### Advantages
3904+
3905+
1. Every Python module has it’s `__name__` defined and if this is `__main__`, it implies that the module is being run standalone by the user and we can do corresponding appropriate actions.
39063906
2. If you import this script as a module in another script, the __name__ is set to the name of the script/module.
39073907
3. Python files can act as either reusable modules, or as standalone programs.
3908-
4. if ```__name__ == “main”:``` is used to execute some code only if the file was run directly, and not imported.
3909-
3908+
4. if `__name__ == “main”:` is used to execute some code only if the file was run directly, and not imported.
39103909

39113910
## Virtual Environment
39123911

python_cheat_sheet.ipynb

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@
198198
"- [Context Manager](#context-manager)\n",
199199
" - [with statement](#with-statement)\n",
200200
" - [Writing your own contextmanager using generator syntax](#writing-your-own-contextmanager-using-generator-syntax)\n",
201+
"- [__main__ Top-level script environment](#main-top-level-script-environment)\n",
202+
" - [Advantages](#advantages)\n",
201203
"- [Virtual Environment](#virtual-environment)\n",
202204
" - [virtualenv](#virtualenv)\n",
203205
" - [pipenv](#pipenv)\n",
@@ -2830,7 +2832,7 @@
28302832
"source": [
28312833
"### set symetric_difference\n",
28322834
"\n",
2833-
"`symetric_difference` or `^` will return all the elements that are different between them."
2835+
"`symetric_difference` or `^` will return all the elements that are not common between them."
28342836
]
28352837
},
28362838
{
@@ -7254,9 +7256,9 @@
72547256
"\n",
72557257
"The names ```args and kwargs``` are arbitrary - the important thing are the ```*``` and ```**``` operators. They can mean:\n",
72567258
"\n",
7257-
"1. In a function declaration, ```*``` means “pack all remaining positional arguments into a tuple named <name>”, while ```**``` is the same for keyword arguments (except it uses a dictionary, not a tuple).\n",
7259+
"1. In a function declaration, ```*``` means “pack all remaining positional arguments into a tuple named `<name>`”, while ```**``` is the same for keyword arguments (except it uses a dictionary, not a tuple).\n",
72587260
"\n",
7259-
"2. In a function call, ```*``` means “unpack tuple or list named <name> to positional arguments at this position”, while ```**``` is the same for keyword arguments.\n",
7261+
"2. In a function call, ```*``` means “unpack tuple or list named `<name>` to positional arguments at this position”, while ```**``` is the same for keyword arguments.\n",
72607262
"\n",
72617263
"For example you can make a function that you can use to call any other function, no matter what parameters it has:"
72627264
]
@@ -7446,6 +7448,69 @@
74467448
"cell_type": "markdown",
74477449
"metadata": {},
74487450
"source": [
7451+
"[*Return to the Top*](#python-cheatsheet)\n",
7452+
"\n",
7453+
"## __main__ Top-level script environment\n",
7454+
"\n",
7455+
"`__main__` is the name of the scope in which top-level code executes.\n",
7456+
"A module’s __name__ is set equal to `__main__` when read from standard input, a script, or from an interactive prompt.\n",
7457+
"\n",
7458+
"A module can discover whether or not it is running in the main scope by checking its own `__name__`, which allows a common idiom for conditionally executing code in a module when it is run as a script or with `python -m` but not when it is imported:"
7459+
]
7460+
},
7461+
{
7462+
"cell_type": "code",
7463+
"execution_count": null,
7464+
"metadata": {},
7465+
"outputs": [],
7466+
"source": [
7467+
">>> if __name__ == \"__main__\":\n",
7468+
"... # execute only if run as a script\n",
7469+
"... main()"
7470+
]
7471+
},
7472+
{
7473+
"cell_type": "markdown",
7474+
"metadata": {},
7475+
"source": [
7476+
"For a package, the same effect can be achieved by including a __main__.py module, the contents of which will be executed when the module is run with -m\n",
7477+
"\n",
7478+
"For example we are developing script which is designed to be used as module, we should do:"
7479+
]
7480+
},
7481+
{
7482+
"cell_type": "code",
7483+
"execution_count": null,
7484+
"metadata": {},
7485+
"outputs": [],
7486+
"source": [
7487+
">>> # Python program to execute function directly\n",
7488+
">>> def add(a, b):\n",
7489+
"... return a+b\n",
7490+
"...\n",
7491+
">>> add(10, 20) # we can test it by calling the function save it as calculate.py\n",
7492+
"30\n",
7493+
">>> # Now if we want to use that module by importing we have to comment out our call,\n",
7494+
">>> # Instead we can write like this in calculate.py\n",
7495+
">>> if __name__ == \"__main__\":\n",
7496+
"... add(3, 5)\n",
7497+
"...\n",
7498+
">>> import calculate\n",
7499+
">>> calculate.add(3, 5)\n",
7500+
"8"
7501+
]
7502+
},
7503+
{
7504+
"cell_type": "markdown",
7505+
"metadata": {},
7506+
"source": [
7507+
"### Advantages\n",
7508+
"\n",
7509+
"1. Every Python module has it’s `__name__` defined and if this is `__main__`, it implies that the module is being run standalone by the user and we can do corresponding appropriate actions.\n",
7510+
"2. If you import this script as a module in another script, the __name__ is set to the name of the script/module.\n",
7511+
"3. Python files can act as either reusable modules, or as standalone programs.\n",
7512+
"4. if `__name__ == “main”:` is used to execute some code only if the file was run directly, and not imported.\n",
7513+
"\n",
74497514
"[*Return to the Top*](#python-cheatsheet)\n",
74507515
"\n",
74517516
"## Virtual Environment\n",

python_cheat_sheet.pdf

4.51 KB
Binary file not shown.

0 commit comments

Comments
 (0)