Python Basics(8)---Modules


1. Modules are programs

  • Any Python program can be imported as a module. You can write a program and store it in a file named hello.py, the name of the file without the extention .py is the module name.
print('hello,world')
  • If you store your file in the path C:\python, to tell the interpreter the position of the file ,you can use the following codes:
import sys
sys.path.append('C:\python') # the path as a string

the path as a string
Then you can import the module hello,

import hello
>>>
hello,world

The module will be exceuted when you import it, but when you do it again,

import hello
>>>

nothing will happend. It is because that modules are meant to define things such as variables, functions, classes and so on. And you only need to define things once, so importing modules several times has the same effect with just import once.

2. Modules Are Used to Define Things

Anything defined in a module can be used as an attributes of the module.

#hello2.py
def hello():
    print('hello,world')
   

import sys
sys.path.append('D:\python')

import hello2
hello2.hello() 
>>>
hello,world

3. Adding Test Code for modules

You want to add test code to examine whether the function inside the module works well.

  • variable ***__name__***: every module(program) has a __name__ variable, when the module is used as the main program, the value of __name__ is ‘__main__’, if a module id used as a imported module, the value of its __name__ because its module name.
__name__ #to check the value inside the main program
>>>
'__main__'

hello2.__name__# to check the value of the imported module
>>>
'hello2'
  • add test code inside the module
def hello():
    print('hello,world')
def test():
    hello()
if __name__=='__main__':
    test()
>>>
hello,world# in the main program, test() will execute



import sys
sys.path.append('D:\python')
import hello3
hello3.hello()
hello3.test()
>>>
hello,world
hello,world

4. Making Your Modules Available

To make your modules found by the intepreter when imported to the program, there are two ways : put the modules in the right place or tell the interpreter where to find them

  • Put the modules in the right place
import sys, pprint
pprint.pprint(sys.path) #pretty --print
>>>
['',
 'D:\\applications\\anaconda\\python36.zip',
 'D:\\applications\\anaconda\\DLLs',
 'D:\\applications\\anaconda\\lib',
 'D:\\applications\\anaconda',
 'D:\\applications\\anaconda\\lib\\site-packages',
 'D:\\applications\\anaconda\\lib\\site-packages\\win32',
 'D:\\applications\\anaconda\\lib\\site-packages\\win32\\lib',
 'D:\\applications\\anaconda\\lib\\site-packages\\Pythonwin',
 'D:\\applications\\anaconda\\lib\\site-packages\\IPython\\extensions',
 'C:\\Users\\.ipython',
 'D:\\python',]

You can use the code above to find the directories you can put your modules

  • Telling the Interpreter Where to Look
    Just like the previous example.

5. Attention on sequence slice

sequence slice : this operation will creat a new sequence object with the elements you choosed from the original one.

a=[1,2,3,4]
b=a[:]
b
>>>
[1,2,3,4]

a[:] copy the elements inside a and creat a new list.

6. Packages

  • To structure your modules, you can group them into a package , a package is just another type of module which can contain other modules. While a module is stored in a file with extention .py, a package is a directory.
  • To put modules into the package, simply put the module file into the package directory. And you can also nest packages into other packages.
  • A package must contain a __init__.py module. And he contents of this file will be the contents of the package, if you import it as if it were a plain module. For example, if you had a package named constants and the file constants/__init__.py contained the statement PI = 3.14
#constants/__init__.py
PI=3.14
import constants
print(constants.PI)
>>>
3.14

7. Exploring Modules

import copy
dir(copy)# everything in the module
>>>
['Error',
'__all__',
'__builtins__',
'__cached__',
'__doc__',
'__file__',
'__loader__',
'__name__',
'__package__',
'__spec__',
'_copy_dispatch',
'_copy_immutable',
'_deepcopy_atomic',
'_deepcopy_dict',
'_deepcopy_dispatch',
'_deepcopy_list',
'_deepcopy_method',
'_deepcopy_tuple',
'_keep_alive',
'_reconstruct',
'copy',
'deepcopy',
'dispatch_table',
'error']

copy.__all__ # everything that * could includes in statement : from copy import *
>>>
['Error', 'copy', 'deepcopy']
  • help(name)
help(copy.copy)
>>>
Help on function copy in module copy:

copy(x)
   Shallow copy operation on arbitrary Python objects.
   
   See the module's __doc__ string for more info.
  • Use the source
print(copy.__file__)
>>>
D:\applications\anaconda\lib\copy.py

Here you get the directory of the module, open the module using the code editor and you will see the source code.

  • Docstring
    __doc__

8. Attention on docstring

A docstring is simply a string you write at the beginning of a function to document it. That string may then be referred to by the function attribute __doc__. As you may understand from the preceding help text, modules may also have docstrings (they are written at the beginning of the module), as may classes (they are written at the beginning of the class).

print(copy.copy.__doc__)
>>>
Shallow copy operation on arbitrary Python objects.

   See the module's __doc__ string for more info.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值