GeezerVison is showing github.com — 学习了python的自定义模块功能 · Xu-Guo/python@2064534
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: README.md +7-1Lines changed: 7 additions & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -4,4 +4,10 @@ gqq python learning |
4 | 4 | - slice2.py 切片技术 |
5 | 5 | - generator.py 生成器 |
6 | 6 | - HigherOrderFunction.py 高阶函数 函数作为返回值 |
7 | | -- decorator.py 装饰器 偏函数 |
| 7 | +- decorator.py 装饰器 偏函数 |
| 8 | +- testhellomodule.py 目录外如何引用自定义模块 |
| 9 | + |
| 10 | +## Module |
| 11 | +- hello.py 定义模块 |
| 12 | +- imagetest.py 测试第三方模块 |
| 13 | +- testhello.py 测试自定义模块 |
Collapse file
Copy file name to clipboard +25Lines changed: 25 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# 第1行注释可以让这个hello.py文件直接在Unix/Linux/Mac上运行,第2行注释表示.py文件本身使用标准UTF-8编码; |
| 4 | +' a test module ' |
| 5 | + |
| 6 | +__author__ = 'GQQ' |
| 7 | + |
| 8 | +import sys |
| 9 | + |
| 10 | +def test(): |
| 11 | + # sys模块有一个argv变量,用list存储了命令行的所有参数。argv至少有一个元素,因为第一个参数永远是该.py文件的名称,例如: |
| 12 | + # 运行python hello.py获得的sys.argv就是['hello.py']; |
| 13 | + # 运行python hello.py Michael获得的sys.argv就是['hello.py', 'Michael]。 |
| 14 | + args = sys.argv |
| 15 | + print len(args) |
| 16 | + if len(args)==1: |
| 17 | + print 'Hello, world!' |
| 18 | + elif len(args)==2: |
| 19 | + print 'Hello, %s!' % args[1] |
| 20 | + else: |
| 21 | + print 'Too many arguments!' |
| 22 | +# 当我们在命令行运行hello模块文件时,Python解释器把一个特殊变量__name__置为__main__, |
| 23 | +# 而如果在其他地方导入该hello模块时,if判断将失败, |
| 24 | +if __name__=='__main__': |
| 25 | + test() |
Collapse file
Copy file name to clipboard Collapse file
Copy file name to clipboard +9Lines changed: 9 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +import Image |
| 2 | + |
| 3 | +im = Image.open('tbyhx.jpg') |
| 4 | + |
| 5 | +print im.format, im.size, im.mode |
| 6 | + |
| 7 | +im.thumbnail((200, 100)) |
| 8 | + |
| 9 | +im.save('thumb.jpg', 'JPEG') |
Collapse file
Copy file name to clipboard 82.2 KB
- Display the source diff
- Display the rich diff
Collapse file
Copy file name to clipboard +7Lines changed: 7 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +# 如何导入模块,就是这么简单 |
| 5 | +import hello |
| 6 | + |
| 7 | +hello.test() |
Collapse file
Copy file name to clipboard 2.36 KB
- Display the source diff
- Display the rich diff
Collapse file
Copy file name to clipboard +11Lines changed: 11 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +# import Python.module.hello |
| 5 | + |
| 6 | +import sys |
| 7 | +# 如何在文件夹外部引用一个模块 |
| 8 | +sys.path.append('module') |
| 9 | +import hello |
| 10 | + |
| 11 | +hello.test() |