Skip to content

Commit

Permalink
updating on modules
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierian-Data committed Feb 24, 2018
1 parent 680c67a commit 1cef96f
Show file tree
Hide file tree
Showing 32 changed files with 148 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@
"source": [
"## lambda expression\n",
"\n",
"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",
"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",
"\n",
"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",
"\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@
"source": [
"## lambda expression\n",
"\n",
"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",
"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",
"\n",
"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",
"\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [
{
"name": "stdout",
Expand Down Expand Up @@ -116,9 +114,7 @@
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [
{
"name": "stdout",
Expand Down Expand Up @@ -171,9 +167,7 @@
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [
{
"name": "stdout",
Expand Down Expand Up @@ -235,9 +229,7 @@
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [
{
"data": {
Expand Down Expand Up @@ -375,27 +367,25 @@
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": false
},
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" | |\n",
" | O | \n",
" | O | O\n",
" | |\n",
"-----------\n",
" | |\n",
" | O | X\n",
" | | \n",
" | |\n",
"-----------\n",
" | |\n",
" X | O | X\n",
" X | X | X\n",
" | |\n",
"Player 2 has won!\n",
"Congratulations! You have won the game!\n",
"Do you want to play again? Enter Yes or No: No\n"
]
}
Expand Down Expand Up @@ -473,7 +463,7 @@
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [default]",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -487,7 +477,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.3"
"version": "3.6.1"
}
},
"nbformat": 4,
Expand Down
5 changes: 5 additions & 0 deletions 06-Modules and Packages/00-Modules_and_Packages/mymodule.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# This is the module we will be importing from!
# Great resource: https://docs.python.org/3/tutorial/modules.html

def func_in_mymodule():
print("I am a function inside of the mymodule.py file!")
34 changes: 34 additions & 0 deletions 06-Modules and Packages/00-Modules_and_Packages/myprogram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
################
# Example Three:
################
# Uncomment this and comment everything else to run!

# import mymodule
# mymodule.func_in_mymodule()

################
# Example Two:
################
# Uncomment this and comment everything else to run!

# import mymodule as mm
# mm.func_in_mymodule()

################
# Example Three:
################
# Uncomment this and comment everything else to run!

# from mymodule import func_in_mymodule
# func_in_mymodule()

################
# Example Four:
################
# Uncomment this and comment everything else to run!

# This is posisble but frowned upon, often causes poorly readable code because
# you don't know what functions come from mymodule

# from mymodule import *
# func_in_mymodule()
76 changes: 76 additions & 0 deletions 06-Modules and Packages/01-Name_and_Main/Explanation.txt
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__.
9 changes: 9 additions & 0 deletions 06-Modules and Packages/01-Name_and_Main/one.py
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")
10 changes: 10 additions & 0 deletions 06-Modules and Packages/01-Name_and_Main/two.py
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.

0 comments on commit 1cef96f

Please sign in to comment.