V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
sunmker
V2EX  ›  Django

关于 reverse 函数

  •  
  •   sunmker · 2018-09-27 13:17:32 +08:00 · 2274 次点击
    这是一个创建于 2010 天前的主题,其中的信息可能已经有所发展或是发生改变。
    不知道为什么 args 参数里面,「 question.id 」后不加逗号就会报错,搜索能力较差,请各位大佬科普这个知识点
    GTim
        1
    GTim  
       2018-09-27 13:36:58 +08:00   ❤️ 1
    因为 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
        2
    dalang  
       2018-09-27 14:22:26 +08:00   ❤️ 1
    ```
    >>> type( ('a') )
    <type 'str'>

    >>> type( ('a',) )
    <type 'tuple'>
    ```
    silhouette
        3
    silhouette  
       2018-09-27 16:30:20 +08:00 via Android
    这个就是 Python 单一元素元组必须要加逗号的问题啊
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3237 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 10:50 · PVG 18:50 · LAX 03:50 · JFK 06:50
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.