关于 reverse 函数

2018-09-27 13:17:32 +08:00
 sunmker
不知道为什么 args 参数里面,「 question.id 」后不加逗号就会报错,搜索能力较差,请各位大佬科普这个知识点
2309 次点击
所在节点    Django
3 条回复
GTim
2018-09-27 13:36:58 +08:00
因为 args 参数要求是一个元组。

Python 中的元组,如果只有一个元素,就需要在后面加逗号,否则会认为是小括号

```python
>>> d = (1)
>>> d
1
>>> d = (1,)
>>> d
(1,)
>>> def function(arg,*args,**kwargs):
print(type(arg))
print(type(args))
print(type(kwargs))


>>> function(1)
<class 'int'>
<class 'tuple'>
<class 'dict'>
```
dalang
2018-09-27 14:22:26 +08:00
```
>>> type( ('a') )
<type 'str'>

>>> type( ('a',) )
<type 'tuple'>
```
silhouette
2018-09-27 16:30:20 +08:00
这个就是 Python 单一元素元组必须要加逗号的问题啊

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/493127

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX