Comments (5)
回答问题:
def keyOnly(a, *b, c)
与 def keyOnly(a, c, *b)
含义不同,前者c必须使用关键字匹配,后者c等同普通参数,可使用关键字匹配也可位置匹配。另外*b是一个tuple,具体可看后面例子。
def keyOnly(a, *, b, c)
里面的*
,我理解的是一个特殊标识,其后所有参数必须使用关键字匹配。
看例子:
# 例1
def keyOnly(a, *b, c):
print(a, b, c)
keyOnly(1, 2, 3) # 报错 | c是必须使用关键字匹配的
keyOnly(1, 2, c=3) # 1 (2,) 3 | a位置匹配了1,c关键字匹配了3,剩余部分都给b
keyOnly(1, 2, 3, c=4) # 1 (2, 3) 4 | a位置匹配了1,c关键字匹配了4,剩余部分都给b
# 例2
def keyOnly(a, *, b, c):
print(a, b, c)`
keyOnly(1, 2, 3) # 报错 | b,c必须关键字匹配
keyOnly(1) # 报错 | b,c不可缺少
keyOnly(1, b=2, c=3) # 1,2,3 | b,c使用了关键字匹配
# 说明:*后面的参数,必须使用关键字匹配,而且在没有默认值的情况下必须使用关键字传入
# 例3
def keyOnly(a, *, b=1):
print(a, b)
keyOnly(1) # 1,1 | 这里跟例2的区别是b有默认值
keyOnly(1, 2) # 报错 | b必须使用关键字匹配
keyOnly(1, b=2) # 1,2 | b使用了关键字匹配
from learnpython.
@Java-via
这样理解都没问题。我也是这样理解。但是实际定义时都是语法错误
In [513]: def keyOnly(a, *b, c):
File "<ipython-input-513-33899a4618ce>", line 1
def keyOnly(a, *b, c):
^
SyntaxError: invalid syntax
In [514]: def keyOnly(a, *, b, c):
File "<ipython-input-514-6c4803bea73d>", line 1
def keyOnly(a, *, b, c):
^
SyntaxError: invalid syntax
In [515]: def keyOnly(a, *, b=1):
File "<ipython-input-515-787a0f504685>", line 1
def keyOnly(a, *, b=1):
^
SyntaxError: invalid syntax
我记得函数参数定义的顺序是:必选参数、默认参数、可变参数和关键字参数,这边的*b
和*
是必须放在后面的。 我用的是2.7,不知道是不是3.x之后没有这个规定?
from learnpython.
2.x版本不支持。
from learnpython.
@xianhu 估计就是这个问题! phper学习python,见笑了,谢谢!
from learnpython.
@xianhu 无限期待3.6向前兼容
from learnpython.
Related Issues (20)
- 有没有捉取图片的py? HOT 1
- 大神,你用的Python3.5吗? HOT 3
- 笔误? HOT 1
- 一行代码输出Mandelbrot图像 HOT 1
- 能否把这篇 demo 翻译成 Python2.7的版本的? HOT 3
- python_lda.py里需要的corpus.txt文件在哪里可以获得?
- Missing Python package numpy in docker request text file HOT 2
- 可能是疏忽了...多了一个括弧 HOT 1
- 既然把调用移除了,那把注释也弄掉吧 HOT 1
- 集合set HOT 1
- pycharm +matplotlib2.02 不能使用交互模式生成动态图 HOT 2
- Change Navbar -> NavbarSimple HOT 1
- python_base.py的295行报错 HOT 1
- Plotly/base.ipynb have a problem
- python_csv.py
- /Library/Fonts/Songti.ttc代码中使用的这个文件可以在哪里获取?
- d HOT 1
- 以撸代码的形式学习Python
- top
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from learnpython.