Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
JackCharles
Python-Code
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
Python-Code
/
module.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
48 lines (37 loc) · 1.77 KB
master
Breadcrumbs
Python-Code
/
module.py
Copy path
Top
File metadata and controls
Code
Blame
48 lines (37 loc) · 1.77 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#!/user/bin/env python3
# coding:utf-8
'a test module'
__author__ = 'JackCharles'
import sys
def test():
args = sys.argv
if len(args) == 1:
print('Hello world!')
elif len(args) == 2:
print('Hello, %s' % args[1])
else:
print('Too many arguments!')
if __name__ == '__main__':
test()
'''
第1行注释可以让这个hello.py文件直接在Unix/Linux/Mac上运行;
第2行注释表示.py文件本身使用标准UTF-8编码;
第4行是文档注释,任何模块代码的第一个字符串都被视为模块的
文档注释,模块定义的文档注释也可以用特殊变量__doc__访问;
第6行使用__author__变量把作者写进去;
以上就是Python模块的标准文件模板,当然也可以全部删掉不写。
sys模块有一个argv变量,用list存储了命令行的所有参数。
argv至少有一个元素,因为第一个参数永远是该.py文件的名称。
if __name__=='__main__':
test()
当我们直接运行module模块时,Python解释器把一个特殊变量
__name__置为__main__,而如果在其他地方导入该模块时,if判断
将失败,因此这种if测试可以让一个模块通过命令行运行时执行一
些额外的代码,最常见的就是测试模块。
类似__xxx__这样的变量是特殊变量,可以被直接引用,但是有特殊用途
类似_xxx和__xxx这样的函数或变量就是非公开的(private),不应该
被直接引用,比如_abc,__abc等;之所以我们说,private函数和变量
“不应该”被直接引用,而不是“不能”被直接引用,是因为Python并没有
一种方法可以完全限制访问private函数或变量,但是,从编程习惯上不
应该引用private函数或变量。
'''
You can’t perform that action at this time.