Skip to content

Commit 2470cb0

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents f5e9915 + 72797a6 commit 2470cb0

File tree

40 files changed

+121
-24
lines changed

40 files changed

+121
-24
lines changed

03-Methods and Functions/.ipynb_checkpoints/05-Lambda-Expressions-Map-and-Filter-checkpoint.ipynb

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@
217217
"source": [
218218
"## lambda expression\n",
219219
"\n",
220-
"One of Pythons most useful (and for recruits, confusing) tools is the lambda expression. lambda expressions allow us to create \"anonymous\" functions. This basically means we can quickly make ad-hoc functions without needing to properly define a function using def.\n",
220+
"One of Pythons most useful (and for beginners, confusing) tools is the lambda expression. lambda expressions allow us to create \"anonymous\" functions. This basically means we can quickly make ad-hoc functions without needing to properly define a function using def.\n",
221221
"\n",
222222
"Function objects returned by running lambda expressions work exactly the same as those created and assigned by defs. There is key difference that makes lambda useful in specialized roles:\n",
223223
"\n",

03-Methods and Functions/05-Lambda-Expressions-Map-and-Filter.ipynb

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@
217217
"source": [
218218
"## lambda expression\n",
219219
"\n",
220-
"One of Pythons most useful (and for recruits, confusing) tools is the lambda expression. lambda expressions allow us to create \"anonymous\" functions. This basically means we can quickly make ad-hoc functions without needing to properly define a function using def.\n",
220+
"One of Pythons most useful (and for beginners, confusing) tools is the lambda expression. lambda expressions allow us to create \"anonymous\" functions. This basically means we can quickly make ad-hoc functions without needing to properly define a function using def.\n",
221221
"\n",
222222
"Function objects returned by running lambda expressions work exactly the same as those created and assigned by defs. There is key difference that makes lambda useful in specialized roles:\n",
223223
"\n",

04-Milestone Project - 1/03-Milestone Project 1 - Complete Walkthrough Solution.ipynb

+12-22
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@
5252
{
5353
"cell_type": "code",
5454
"execution_count": 2,
55-
"metadata": {
56-
"collapsed": false
57-
},
55+
"metadata": {},
5856
"outputs": [
5957
{
6058
"name": "stdout",
@@ -116,9 +114,7 @@
116114
{
117115
"cell_type": "code",
118116
"execution_count": 4,
119-
"metadata": {
120-
"collapsed": false
121-
},
117+
"metadata": {},
122118
"outputs": [
123119
{
124120
"name": "stdout",
@@ -171,9 +167,7 @@
171167
{
172168
"cell_type": "code",
173169
"execution_count": 6,
174-
"metadata": {
175-
"collapsed": false
176-
},
170+
"metadata": {},
177171
"outputs": [
178172
{
179173
"name": "stdout",
@@ -235,9 +229,7 @@
235229
{
236230
"cell_type": "code",
237231
"execution_count": 8,
238-
"metadata": {
239-
"collapsed": false
240-
},
232+
"metadata": {},
241233
"outputs": [
242234
{
243235
"data": {
@@ -375,27 +367,25 @@
375367
},
376368
{
377369
"cell_type": "code",
378-
"execution_count": 16,
379-
"metadata": {
380-
"collapsed": false
381-
},
370+
"execution_count": 14,
371+
"metadata": {},
382372
"outputs": [
383373
{
384374
"name": "stdout",
385375
"output_type": "stream",
386376
"text": [
387377
" | |\n",
388-
" | O | \n",
378+
" | O | O\n",
389379
" | |\n",
390380
"-----------\n",
391381
" | |\n",
392-
" | O | X\n",
382+
" | | \n",
393383
" | |\n",
394384
"-----------\n",
395385
" | |\n",
396-
" X | O | X\n",
386+
" X | X | X\n",
397387
" | |\n",
398-
"Player 2 has won!\n",
388+
"Congratulations! You have won the game!\n",
399389
"Do you want to play again? Enter Yes or No: No\n"
400390
]
401391
}
@@ -473,7 +463,7 @@
473463
"metadata": {
474464
"anaconda-cloud": {},
475465
"kernelspec": {
476-
"display_name": "Python [default]",
466+
"display_name": "Python 3",
477467
"language": "python",
478468
"name": "python3"
479469
},
@@ -487,7 +477,7 @@
487477
"name": "python",
488478
"nbconvert_exporter": "python",
489479
"pygments_lexer": "ipython3",
490-
"version": "3.5.3"
480+
"version": "3.6.1"
491481
}
492482
},
493483
"nbformat": 4,

06-Modules and Packages/00-Modules_and_Packages/MyMainPackage/SubPackage/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def sub_report():
2+
print("Hey Im a function inside mysubscript")

06-Modules and Packages/00-Modules_and_Packages/MyMainPackage/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def report_main():
2+
print("Hey I am in some_main_script in main package.")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def my_func():
2+
print("Hey I am in mymodule.py")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from MyMainPackage.some_main_script import report_main
2+
from MyMainPackage.SubPackage import mysubscript
3+
4+
report_main()
5+
6+
mysubscript.sub_report()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
Sometimes when you are importing from a module, you would like to know whether
2+
a modules function is being used as an import, or if you are using the original
3+
.py file of that module. In this case we can use the:
4+
5+
if __name__ == "__main__":
6+
7+
line to determine this. For example:
8+
9+
When your script is run by passing it as a command to the Python interpreter:
10+
11+
python myscript.py
12+
13+
all of the code that is at indentation level 0 gets executed. Functions and
14+
classes that are defined are, well, defined, but none of their code gets ran.
15+
Unlike other languages, there's no main() function that gets run automatically
16+
- the main() function is implicitly all the code at the top level.
17+
18+
In this case, the top-level code is an if block. __name__ is a built-in variable
19+
which evaluate to the name of the current module. However, if a module is being
20+
run directly (as in myscript.py above), then __name__ instead is set to the
21+
string "__main__". Thus, you can test whether your script is being run directly
22+
or being imported by something else by testing
23+
24+
if __name__ == "__main__":
25+
...
26+
27+
If that code is being imported into another module, the various function and
28+
class definitions will be imported, but the main() code won't get run. As a
29+
basic example, consider the following two scripts:
30+
31+
# file one.py
32+
def func():
33+
print("func() in one.py")
34+
35+
print("top-level in one.py")
36+
37+
if __name__ == "__main__":
38+
print("one.py is being run directly")
39+
else:
40+
print("one.py is being imported into another module")
41+
42+
and then:
43+
44+
# file two.py
45+
import one
46+
47+
print("top-level in two.py")
48+
one.func()
49+
50+
if __name__ == "__main__":
51+
print("two.py is being run directly")
52+
else:
53+
print("two.py is being imported into another module")
54+
55+
Now, if you invoke the interpreter as
56+
57+
python one.py
58+
59+
The output will be
60+
61+
top-level in one.py
62+
63+
one.py is being run directly
64+
If you run two.py instead:
65+
66+
python two.py
67+
68+
You get
69+
70+
top-level in one.py
71+
one.py is being imported into another module
72+
top-level in two.py
73+
func() in one.py
74+
two.py is being run directly
75+
76+
Thus, when module one gets loaded, its __name__ equals "one" instead of __main__.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
def func():
2+
print("func() ran in one.py")
3+
4+
print("top-level print inside of one.py")
5+
6+
if __name__ == "__main__":
7+
print("one.py is being run directly")
8+
else:
9+
print("one.py is being imported into another module")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import one
2+
3+
print("top-level in two.py")
4+
5+
one.func()
6+
7+
if __name__ == "__main__":
8+
print("two.py is being run directly")
9+
else:
10+
print("two.py is being imported into another module")

0 commit comments

Comments
 (0)