-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12a_modules.py
More file actions
executable file
·48 lines (36 loc) · 1.5 KB
/
12a_modules.py
File metadata and controls
executable file
·48 lines (36 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# A module is simply a python file (with .py extension)
# A module is imported by using import keyword
# Modules are loaded only once and initialized only once
# i.e. if the other module imports the same module, it will not be initialized again
#
# so local variables inside the module act as a "singleton" - they are initialized only once.
import urllib
import mymodule
import mymodule2
connection = urllib.urlopen("http://www.learnpython.org/en/Modules_and_Packages")
#print "result = " + str(connection.info())
print "result code = "+str(connection.geturl())
# To make your own module, you just need to create a file with the module name
# To import the module, just use import command and filename without the extension
# To use the functions in the module, use <modulename>.<functionname>
print "Magic number in the module: %d" % mymodule.magic_number
print mymodule.add(3,4)
print mymodule.subtract(3,4)
print mymodule.divide(3,4)
print mymodule.multiply(3,4)
# You can import one module inside the other
from mymodule2 import average
# mymodule2 loads mymodule. But we'd expect that mymodule is loaded only once.
# this should NOT print 2
print "Magic number in the module: %d" % mymodule.magic_number
list = [1,2,5,7,8,10,15, -3]
x = mymodule2.average(list)
print x
# Exercise
# In this exercise, you will need to print an alphabetically sorted list of all functions in the re module,
# which contain the word find.
import re
#import dir
function_list = dir(re)
for function in function_list :
print function