【问题标题】:what is * arguments (without suffix) function/method in python? [duplicate]python中的*参数(不带后缀)函数/方法是什么? [复制]
【发布时间】:2018-10-17 00:24:47
【问题描述】:

我最近在几个项目上工作,看到了一个新的语法代码,一个获取 * 参数的方法(我知道 *args 和 **kwargs)

以 django 2.0.4 为例:

class DataListView(ListView):
    ...
    def get_context_data(self, *, object_list=None, **kwargs): # * argument without suffix
        return super().get_context_data(object_list, **kwargs)

问题:python函数/方法中的*参数是什么意思?

【问题讨论】:

    标签: python


    【解决方案1】:

    所以object_list 必须是一个命名参数。所有位置参数都将被* 捕获并忽略。

    这是在 python 3.0 中添加的,并在 PEP 3132 -- Extended Iterable Unpacking 中进行了描述

    【讨论】:

    • 您能为此分享任何文件吗?
    • 我用相关 PEP 的链接更新了答案。
    猜你喜欢
    • 2014-04-07
    • 1970-01-01
    • 2013-10-31
    • 2016-07-14
    • 2016-04-30
    • 1970-01-01
    • 1970-01-01
    • 2012-09-02
    • 2018-05-12
    相关资源
    最近更新 更多