GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — 学习了python的自定义模块功能 · Xu-Guo/python@2064534

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • README.md
  • module
    • hello.pyc
    • hello.py
    • imagetest.py
    • tbyhx.jpg
    • testhello.py
    • thumb.jpg
  • testhellomodule.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎README.md‎

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 numberDiff line numberDiff line change
@@ -4,4 +4,10 @@ gqq python learning
44
- slice2.py 切片技术
55
- generator.py 生成器
66
- 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

‎module/hello.py‎

Copy file name to clipboard
+25Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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

‎module/hello.pyc‎

Copy file name to clipboard
545 Bytes
Binary file not shown.
Collapse file

‎module/imagetest.py‎

Copy file name to clipboard
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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

‎module/tbyhx.jpg‎

Copy file name to clipboard
82.2 KB
  • Display the source diff
  • Display the rich diff
Loading
Collapse file

‎module/testhello.py‎

Copy file name to clipboard
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
# 如何导入模块,就是这么简单
5+
import hello
6+
7+
hello.test()
Collapse file

‎module/thumb.jpg‎

Copy file name to clipboard
2.36 KB
  • Display the source diff
  • Display the rich diff
Loading
Collapse file

‎testhellomodule.py‎

Copy file name to clipboard
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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()

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.