python - def方法(参数=无)是什么意思?

标签 python

<分区>

class Employee:
    def __init__(self, lastname, firstname = None):
        self.lastname = lastname
        self.firstname = firstname

第二行的 firstname = None 是什么意思?

最佳答案

这允许您调用此函数并省略该特定参数。如果您确实省略了它,它将默认为列出的值 None。如果您确实传递了一个值,它将被使用。

>>> def f(arg='a'):
...     print(arg)
...
>>> f()
a
>>> f('b')
b

关于python - def方法(参数=无)是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36418542/

相关文章:

python - 如何禁用 Django UserCreationForm 中用户名字段的自动对焦?

python - "not =="和 "!="有什么区别?

Python从sourceforge获取文件

python - 如何在序列化程序创建方法中获取 URL Id?

Python只读取excel单元格中的公式

python - 无法调整 OpenCV 的 hog.detectMultiScale 中的分组设置(python)

python - 如何用循环创建变量?

python - ffmpeg命令抛出 "Invalid duration specification for ss: 00:00:00,000"

python - 如何在不使用pip的情况下安装pyqt5和pyqt5-tools?

python - 带有 SVM 的 GridSearch 产生 IndexError