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 }}
jinweizhao
Python-Learning
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
Python-Learning
/
hello.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
64 lines (36 loc) · 1.95 KB
master
Breadcrumbs
Python-Learning
/
hello.py
Copy path
Top
File metadata and controls
Code
Blame
64 lines (36 loc) · 1.95 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'a test module' #一个字符串,表示模块的文档注释,任何模块代码的第一个字符串都被视为模块的文档注释
__author__ = 'jwz' #使用__author__变量把作者写进去,这样当你公开源代码后别人就可以瞻仰你的大名
import sys
def test():
args = sys.argv
print(args)
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()
# 当我们在命令行运行hello模块文件时,Python解释器把一个特殊变量__name__置为__main__,
# 而如果在其他地方导入该hello模块时,if判断将失败,因此,
# 这种if测试可以让一个模块通过命令行运行时执行一些额外的代码,最常见的就是运行测试。
# 仅仅在模块内部使用。在Python中,是通过_前缀来实现的
# 正常的函数和变量名是公开的(public)
# 类似__xxx__这样的变量是特殊变量,可以被直接引用,但是有特殊用途,比如上面的__author__,
# __name__就是特殊变量,hello模块定义的文档注释也可以用特殊变量__doc__访问,我们自己的变量一般不要用这种变量名;
# 类似_xxx和__xxx这样的函数或变量就是非公开的(private),不应该被直接引用,比如_abc,__abc等
# 我们说,private函数和变量“不应该”被直接引用,而不是“不能”被直接引用,
# 是因为Python并没有一种方法可以完全限制访问private函数或变量,但是,从编程习惯上不应该引用private函数或变量
# private函数或变量不应该被别人引用,那它们有什么用呢?请看例子:
def _private_1(name):
return 'Hello, %s' % name
def _private_2(name):
return 'Hi, %s' % name
def greeting(name):
if len(name) > 3:
return _private_1(name)
else:
return _private_2(name)
You can’t perform that action at this time.