Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ac3bd23

Browse files
committedJan 12, 2022
Added task Executing Modules as scripts
1 parent cdecd9d commit ac3bd23

File tree

8 files changed

+139
-1
lines changed

8 files changed

+139
-1
lines changed
 

‎Modules and packages/Executing modules as scripts/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def func():
2+
print('This is a message from the function in the imported module.')
3+
4+
5+
print(f'This is a message from {__name__}.')
6+
7+
# Make a change here.
8+
if __name__ == "__main__":
9+
print('This should not be printed')
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
type: edu
2+
files:
3+
- name: task.py
4+
visible: true
5+
placeholders:
6+
- offset: 65
7+
length: 18
8+
placeholder_text: '# Call func() from the imported module'
9+
- offset: 107
10+
length: 100
11+
placeholder_text: print('This should be printed ONLY when task.py is called directly.')
12+
- name: tests/test_task.py
13+
visible: false
14+
- name: __init__.py
15+
visible: false
16+
- name: tests/__init__.py
17+
visible: false
18+
- name: some_module.py
19+
visible: true
20+
placeholders:
21+
- offset: 155
22+
length: 66
23+
placeholder_text: print('This should not be printed')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
## Executing modules as scripts
2+
3+
A Python module is a file containing executable statements as well as function or class definitions.
4+
These statements are executed the first time the module name is encountered in an `import` statement.
5+
The file name is the module name with the suffix .py appended. Within a module, the
6+
module’s name (as a string) is available as the value of the global variable `__name__`.
7+
A module can contain executable statements as well as function or class definitions.
8+
These statements are executed the first time the module name is encountered in an `import` statement.
9+
10+
When you run a module **directly** (that is, not by importing it into another one),
11+
the code in the module will be executed, just as if you imported it, but with the
12+
`__name__` set to `"__main__"`.
13+
14+
You can use `__name__` and `__main__` like this:
15+
16+
```python
17+
if __name__ == "__main__":
18+
# Do something here
19+
```
20+
21+
The statements inside this block will be executed only if the module is run directly and not through import
22+
into another module. For example, let's consider two files:
23+
24+
main_program:
25+
```python
26+
import some_module
27+
28+
print(f"main_program __name__ is: {__name__}")
29+
30+
if __name__ == "__main__":
31+
print("main_program executed directly")
32+
else:
33+
print("Main program executed when imported")
34+
```
35+
36+
some_module:
37+
```python
38+
print(f"some_module __name__ is: {__name__}")
39+
40+
if __name__ == "__main__":
41+
print("some_module executed directly")
42+
else:
43+
print("some_module executed when imported")
44+
```
45+
46+
Output after running `main_program`:
47+
```text
48+
some_module __name__ is: some_module
49+
some_module executed when imported
50+
main_program __name__ is: __main__
51+
main_program executed directly
52+
```
53+
54+
Output after running `some_module`:
55+
```text
56+
some_module __name__ is: __main__
57+
some_module executed directly
58+
```
59+
60+
Modify `task.py` and `some_module.py` so that when you run task.py your output is as follows:
61+
62+
```text
63+
This is a message from some_module.
64+
This is a message from __main__.
65+
This is a message from the function in the imported module.
66+
This should be printed ONLY when task.py is called directly.
67+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import some_module
2+
3+
print(f'This is a message from {__name__}.')
4+
some_module.func()
5+
6+
# Make a change here.
7+
if __name__ == "__main__":
8+
print('This should be printed ONLY when task.py is called directly.')
9+

‎Modules and packages/Executing modules as scripts/tests/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import unittest
2+
import contextlib
3+
import io
4+
5+
6+
f = io.StringIO()
7+
with contextlib.redirect_stdout(f):
8+
import task
9+
10+
output = f.getvalue().split('\n')
11+
12+
13+
class TestCase1(unittest.TestCase):
14+
def test_some_module(self):
15+
test_out = ['This is a message from some_module.', 'This is a message from task.', 'This is a message from the function in the imported module.', '']
16+
self.assertEqual(test_out, output, msg='Something is wrong, actual output does not match expected.')
17+
18+
def test_some_main(self):
19+
self.assertFalse("This should not be printed" in output,
20+
msg='You should move the last print statement in some_module into the `main` block.')
21+
22+
def test_main_main(self):
23+
self.assertFalse("This should be printed ONLY when task.py is called directly." in output,
24+
msg='You should move the last print statement in task.py into the `main` block.')
25+
26+
def test_main_func(self):
27+
self.assertTrue('This is a message from the function in the imported module.' in output,
28+
msg='Do not forget to call the function from the imported module.')

‎Modules and packages/lesson-info.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ content:
22
- Import module
33
- Built-in modules
44
- From import
5-
- Packages
5+
- Packages
6+
- Executing modules as scripts

0 commit comments

Comments
 (0)
Please sign in to comment.