gpt4 book ai didi

python - 为什么设置默认参数然后在python中运行if语句?

转载 作者:太空宇宙 更新时间:2023-11-04 09:31:38 25 4
gpt4 key购买 nike

我遇到的代码如下所示:

def plot_svc(model, ax=None, plot_support=True):
"""function to plot 2-D SVC"""
if ax is None:
ax=plt.gca()

如果我们要设置默认参数 ax=None 为什么要使用 if 语句,难道我们不知道 axNone 吗?所以我们不能只写:

def plot_svc(model, ax=None, plot_support=True):
"""function to plot 2-D SVC"""
ax=plt.gca()

谢谢!

最佳答案

因为调用函数时ax可能不是None。

def f(x=None):
if x is None:
return True
return False

>>> f(None)
True
>>> f()
True
>>> f(3)
False

关于python - 为什么设置默认参数然后在python中运行if语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55505761/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com