Skip to content

Commit 60ff9ac

Browse files
committed
added module and packages tutorial
1 parent 49063c7 commit 60ff9ac

File tree

9 files changed

+55
-0
lines changed

9 files changed

+55
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import urllib
99
import mymodule
10+
import mymodule2
1011

1112
connection = urllib.urlopen("http://www.learnpython.org/en/Modules_and_Packages")
1213
#print "result = " + str(connection.info())
@@ -16,8 +17,32 @@
1617

1718
# To import the module, just use import command and filename without the extension
1819
# To use the functions in the module, use <modulename>.<functionname>
20+
print "Magic number in the module: %d" % mymodule.magic_number
1921

2022
print mymodule.add(3,4)
2123
print mymodule.subtract(3,4)
2224
print mymodule.divide(3,4)
2325
print mymodule.multiply(3,4)
26+
27+
# You can import one module inside the other
28+
from mymodule2 import average
29+
30+
# mymodule2 loads mymodule. But we'd expect that mymodule is loaded only once.
31+
# this should NOT print 2
32+
print "Magic number in the module: %d" % mymodule.magic_number
33+
34+
list = [1,2,5,7,8,10,15, -3]
35+
x = mymodule2.average(list)
36+
37+
print x
38+
39+
40+
# Exercise
41+
# In this exercise, you will need to print an alphabetically sorted list of all functions in the re module,
42+
# which contain the word find.
43+
import re
44+
45+
#import dir
46+
function_list = dir(re)
47+
for function in function_list :
48+
print function

python2/12b_packages.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# packages are simply directories that can contain modules and other packages
2+
# the only secret is that you have to create __init__.py module inside the package directory
3+
4+
# the following works, but requies you to specify package name when calling a function
5+
import mypackage.p_mymodule
6+
7+
# like this...
8+
mypackage.p_mymodule.greeting()
9+
10+
11+
# the following works too and does not require you to use the package name
12+
from mypackage import p_mymodule
13+
14+
p_mymodule.greeting()

python2/generators.py

Whitespace-only changes.

python2/mymodule.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
2+
magic_number = 0
3+
magic_number += 1
4+
15
def add(x,y):
26
return x+y
37

python2/mymodule2.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import mymodule
2+
3+
def average(list):
4+
sum = 0
5+
6+
for x in list:
7+
sum = mymodule.add(sum, x)
8+
return sum/len(list)

python2/mypackage/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# This file does not necessarily have to contain any code

python2/mypackage/__init__.pyc

158 Bytes
Binary file not shown.

python2/mypackage/p_mymodule.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
def greeting() :
3+
print "hello there!"

python2/mypackage/p_mymodule.pyc

350 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)