在函数中将等于"None"的关键字作为参数传递的意义是什么? [英] What is the point of passing keywords equal to `None` as parameters in a function?

查看:72
本文介绍了在函数中将等于"None"的关键字作为参数传递的意义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 Python Voronoi Generator 转换为Java但是我对关键字参数不是很熟悉,我对作为函数参数编写的关键字参数感到困惑.

I am trying to translate a Python Voronoi Generator to Java but I am not very familiar with keyword arguments and I am confused by the keyword argument that is written as a parameter of a function.

我已阅读此问题,但仅涵盖调用参数,而没有定义它们.

I've read this question, but it only covers calling arguments, not defining them.

似乎该功能只是将ab设置为无.我不明白为什么这是必要的.为什么不直接输入None而不使用类似的参数?

It seems like the function is just setting a and b to None. I don't understand why that is necessary. Why not just type None instead of using parameters like that?

在函数定义中使用等于None的关键字参数有什么意义?

功能:

class Arc:
    p = None
    pprev = None
    pnext = None
    e = None
    s0 = None
    s1 = None

    def __init__(self, p, a=None, b=None):
        self.p = p
        self.pprev = a
        self.pnext = b
        self.e = None
        self.s0 = None
        self.s1 = None

推荐答案

关键字参数可以具有默认值.在这种情况下,ab的默认值为None.这意味着当不将这些参数传递到函数中时,ab被设置为None.

Keyword arguments can have a default value. In this case, the default value of a and b are None. This means that when these arguments aren't passed into the function, a and b are set to None.

示例:

def func(a=None):
    print(a)
func()
>>>None

但是,如果您执行以下操作:

But if you do something like this:

def func(a):
    print(a)
func()
>>>TypeError: func() missing 1 required positional argument: 'a'

这篇关于在函数中将等于"None"的关键字作为参数传递的意义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆