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 7af746d

Browse files
committedJun 17, 2021
Updated From import
1 parent 8d8849e commit 7af746d

File tree

6 files changed

+90
-18
lines changed

6 files changed

+90
-18
lines changed
 
Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,36 @@
11
"""
2-
This module contains Calculator class
2+
This module contains calculator classes
33
"""
44

55

6-
class Calculator:
6+
class Add:
77
def __init__(self):
88
self.current = 0
99

1010
def add(self, amount):
1111
self.current += amount
1212

13+
def get_current(self):
14+
return self.current
15+
16+
17+
class Subtract:
18+
def __init__(self):
19+
self.current = 0
20+
21+
def subtract(self, amount):
22+
self.current -= amount
23+
24+
def get_current(self):
25+
return self.current
26+
27+
28+
class Multiply:
29+
def __init__(self):
30+
self.current = 0
31+
32+
def multiply(self, amount):
33+
self.current *= amount
34+
1335
def get_current(self):
1436
return self.current
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
from calculator import Add
2+
from my_module import hello as hey
13

2-
from calculator import Calculator
34

4-
calc = Calculator() # here we can use Calculator class directly without prefix calculator.
5+
calc = Add() # name `Calculator` used directly without prefix `calculator`
56
calc.add(2)
67
print(calc.get_current())
78

8-
from my_module import hello_world
99

10-
print(hello_world()) # Note: hello_world function used without prefix
10+
print(hey("User"))
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
""" documentation string for module my_module
2-
This module contains hello_world function
2+
This module contains hello function
33
"""
44

55

6-
def hello_world():
7-
return "Hello, World!"
6+
def hello(name):
7+
return f"Hey there, {name}!"

‎Modules and packages/From import/task-info.yaml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ files:
77
- name: from_import.py
88
visible: true
99
placeholders:
10-
- offset: 169
11-
length: 33
12-
placeholder_text: import hello_world from my_module
10+
- offset: 186
11+
length: 11
12+
placeholder_text: '# call the imported function to print your name'
13+
- offset: 27
14+
length: 34
15+
placeholder_text: '# Import the function here'
1316
- name: tests.py
1417
visible: false
Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,45 @@
11
## from import
22

3-
One form of the import statement imports names `from` a module directly into the importing module's symbol table. This way you can use the imported name directly without the `module_name` prefix.
4-
5-
Import `hello_world` function from `my_module` . Check the difference with task1.
3+
One form of the import statement imports names `from` a module directly into the
4+
importing module's symbol table. This way you can use the imported name directly
5+
without the `module_name` prefix. For example:
66

7-
<div class='hint'>Use 'from my_module import hello_world'</div>
7+
```python
8+
from calculator import Add
9+
10+
calc = Add() # name `Calculator` used directly without prefix `calculator`
11+
```
12+
13+
This does not introduce the module name from which the imports are taken in the
14+
local symbol table (so in our example, `calculator` is not defined).
15+
16+
There is even a variant to import all names that a module defines:
17+
```python
18+
from calculator import *
19+
calc = Multiply()
20+
```
21+
This imports all names (except those beginning with an underscore `_`).
22+
In most cases Python programmers do not use this facility since it introduces
23+
an unknown set of names into the interpreter, possibly hiding some things
24+
you have already defined.
25+
26+
If the module name is followed by as, then the name following as is bound
27+
directly to the imported module:
28+
29+
```python
30+
import my_module as mm
31+
mm.hello_world()
32+
```
33+
This is effectively importing the module in the same way that `import my_module` will
34+
do, with the only difference of it being available as `mm`. It can also be used
35+
when utilising `from` with similar effects:
36+
37+
```python
38+
from calculator import Subtract as Minus
39+
```
40+
41+
Import the `hello` function from `my_module` as `hey` and call it to print your name.
42+
43+
<div class='hint'>Use syntax such as <code>from some_module import some_func as func</code>.</div>
44+
<div class="hint">Note: <code>hey</code> function should be called without a prefix. The function expects
45+
one string argument.</div>

‎Modules and packages/From import/tests.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,19 @@
33

44
def test_window():
55
window = get_answer_placeholders()[0]
6-
if "from " in window:
6+
if "from" in window:
77
passed()
88
else:
9-
failed("Use hello_world imported from my_module")
9+
failed("Wrong import, check out hint 1")
10+
11+
12+
def test_window_1():
13+
window = get_answer_placeholders()[1]
14+
if "hey('" in window or 'hey("' in window:
15+
passed()
16+
else:
17+
failed("Call the hey function and give it our name as a string!")
18+
1019

1120
if __name__ == '__main__':
1221
run_common_tests()

0 commit comments

Comments
 (0)
Please sign in to comment.