forked from Pierian-Data/Complete-Python-3-Bootcamp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/master'
- Loading branch information
Showing
40 changed files
with
121 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Empty file.
Binary file added
BIN
+144 Bytes
...ages/00-Modules_and_Packages/MyMainPackage/SubPackage/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file added
BIN
+293 Bytes
...s/00-Modules_and_Packages/MyMainPackage/SubPackage/__pycache__/mysubscript.cpython-36.pyc
Binary file not shown.
2 changes: 2 additions & 0 deletions
2
06-Modules and Packages/00-Modules_and_Packages/MyMainPackage/SubPackage/mysubscript.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
def sub_report(): | ||
print("Hey Im a function inside mysubscript") |
Empty file.
Binary file added
BIN
+133 Bytes
...es and Packages/00-Modules_and_Packages/MyMainPackage/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file added
BIN
+297 Bytes
...ackages/00-Modules_and_Packages/MyMainPackage/__pycache__/some_main_script.cpython-36.pyc
Binary file not shown.
2 changes: 2 additions & 0 deletions
2
06-Modules and Packages/00-Modules_and_Packages/MyMainPackage/some_main_script.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
def report_main(): | ||
print("Hey I am in some_main_script in main package.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
def my_func(): | ||
print("Hey I am in mymodule.py") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from MyMainPackage.some_main_script import report_main | ||
from MyMainPackage.SubPackage import mysubscript | ||
|
||
report_main() | ||
|
||
mysubscript.sub_report() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
Sometimes when you are importing from a module, you would like to know whether | ||
a modules function is being used as an import, or if you are using the original | ||
.py file of that module. In this case we can use the: | ||
|
||
if __name__ == "__main__": | ||
|
||
line to determine this. For example: | ||
|
||
When your script is run by passing it as a command to the Python interpreter: | ||
|
||
python myscript.py | ||
|
||
all of the code that is at indentation level 0 gets executed. Functions and | ||
classes that are defined are, well, defined, but none of their code gets ran. | ||
Unlike other languages, there's no main() function that gets run automatically | ||
- the main() function is implicitly all the code at the top level. | ||
|
||
In this case, the top-level code is an if block. __name__ is a built-in variable | ||
which evaluate to the name of the current module. However, if a module is being | ||
run directly (as in myscript.py above), then __name__ instead is set to the | ||
string "__main__". Thus, you can test whether your script is being run directly | ||
or being imported by something else by testing | ||
|
||
if __name__ == "__main__": | ||
... | ||
|
||
If that code is being imported into another module, the various function and | ||
class definitions will be imported, but the main() code won't get run. As a | ||
basic example, consider the following two scripts: | ||
|
||
# file one.py | ||
def func(): | ||
print("func() in one.py") | ||
|
||
print("top-level in one.py") | ||
|
||
if __name__ == "__main__": | ||
print("one.py is being run directly") | ||
else: | ||
print("one.py is being imported into another module") | ||
|
||
and then: | ||
|
||
# file two.py | ||
import one | ||
|
||
print("top-level in two.py") | ||
one.func() | ||
|
||
if __name__ == "__main__": | ||
print("two.py is being run directly") | ||
else: | ||
print("two.py is being imported into another module") | ||
|
||
Now, if you invoke the interpreter as | ||
|
||
python one.py | ||
|
||
The output will be | ||
|
||
top-level in one.py | ||
|
||
one.py is being run directly | ||
If you run two.py instead: | ||
|
||
python two.py | ||
|
||
You get | ||
|
||
top-level in one.py | ||
one.py is being imported into another module | ||
top-level in two.py | ||
func() in one.py | ||
two.py is being run directly | ||
|
||
Thus, when module one gets loaded, its __name__ equals "one" instead of __main__. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
def func(): | ||
print("func() ran in one.py") | ||
|
||
print("top-level print inside of one.py") | ||
|
||
if __name__ == "__main__": | ||
print("one.py is being run directly") | ||
else: | ||
print("one.py is being imported into another module") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import one | ||
|
||
print("top-level in two.py") | ||
|
||
one.func() | ||
|
||
if __name__ == "__main__": | ||
print("two.py is being run directly") | ||
else: | ||
print("two.py is being imported into another module") |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.