Skip to content

Commit a79ed25

Browse files
committed
docs: top-level code environment definition and usecases.
1 parent 60605e1 commit a79ed25

18 files changed

Lines changed: 125 additions & 31 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,5 @@ cython_debug/
163163
.vscode
164164

165165
ecosystem/
166-
scratch_pad.py
166+
scratch_pad.py
167+
journal

.spellcheck_exceptions_dictionary.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ formatspec
7171
sys
7272
iterable
7373
init
74+
metaclasses
75+
hasattr
7476

7577

7678
# technology:
@@ -205,4 +207,4 @@ CircuitPython
205207

206208

207209
# domain specific - C/Python API
208-
PyTypeObject
210+
PyTypeObject
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def python_runtime_services:
2+
- formal: ???
3+
4+
- in words: ???
5+
6+
- plain english: modules to interact with the Python interpreter and its runtime environment.
7+
8+
- intuition: ???
9+
10+
- properties: ???
11+
12+
- examples: ???
13+
14+
- use cases: ???
15+
16+
- proof: ???
17+
18+
References: ???
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
def __main__"
2+
- formal: ???
3+
4+
- in words:
5+
- name for two Python constructs:
6+
1. default name of the top-level environment.
7+
2. __main__.py in Python packages
8+
9+
- plain english:
10+
1. default name of the entry-point of a Python program:
11+
- akin to Java's: public static void main, C's int main, etc
12+
2. __main__.py in Python packages: entry point for package when ran using `python -m package_name`
13+
14+
15+
- intuition: ???
16+
17+
- properties:
18+
19+
20+
- examples:
21+
see: 4_experiments/2_standard_library/29_python_runtime_services/5_top_level_code_environment
22+
23+
- use cases:
24+
- a module can use it to detect if it is the entry-point of the program, with the idiomatic pattern below:
25+
``` python
26+
if __name__ == __main__:
27+
# perform entry-point code
28+
```
29+
30+
- access top-level(entry-point) module's namespace
31+
- in practices usages:
32+
- pdb: https://docs.python.org/3/library/pdb.html#module-pdb
33+
- rlcompleter: https://docs.python.org/3/library/rlcompleter.html#module-rlcompleter
34+
35+
see: 4_experiments/2_standard_library/29_python_runtime_services/5_top_level_code_environment/1_top_level_code/2_top_level_environment_namespace/test_entry_point.sh
36+
37+
- provide a command line interface to a package.
38+
see: 4_experiments/2_standard_library/29_python_runtime_services/5_top_level_code_environment/2_package_entry_point/test_package_main.sh
39+
40+
41+
- proof: ???
42+
43+
References: ???
44+
Python Software Foundation. 2025. Python Runtime Services, The Python Standard Library. https://docs.python.org/3/library/__main__.html#
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from foo import foo
2+
3+
4+
def bar():
5+
print("In bar function")
6+
print(f"top-level code module: {__name__}")
7+
foo()
8+
9+
10+
if __name__ == "__main__":
11+
print("In bar module, as top-level code environment")
12+
bar()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""
2+
top level code is also called the entry-point.
3+
"""
4+
5+
6+
def foo():
7+
print("In foo function")
8+
print(f"top-level code module: {__name__}")
9+
10+
11+
if __name__ == "__main__":
12+
print("In foo module, as top-level code environment")
13+
foo()

4_experiments/2_standard_library/29_python_runtime_services/5_top_level_code_environment/1_top_level_code/2_top_level_environment_namespace/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from sub_module import print_entry_point_state
2+
3+
variable = None
4+
5+
if __name__ == "__main__":
6+
variable = input("Please supply a value: ")
7+
print_entry_point_state()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import __main__
2+
3+
4+
def print_entry_point_state():
5+
print(
6+
f"Printing a value set in __main__(i.e. top-level | entry-point) module. Value is {__main__.variable}")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!sh
2+
3+
python -m entry_point

0 commit comments

Comments
 (0)