python __defaults__
时间: 2023-10-16 16:28:01 浏览: 128
`__defaults__` is a built-in attribute of Python functions that contains a tuple of default argument values for the function. It is used to access the default values of the function arguments.
For example, consider the following function:
```
def multiply(a, b=1):
return a * b
```
Here, `a` is a mandatory argument and `b` is an optional argument with a default value of 1. The default value of `b` can be accessed using the `__defaults__` attribute:
```
>>> multiply.__defaults__
(1,)
```
The `__defaults__` attribute returns a tuple containing the default value of `b`. In this case, the tuple contains only one element, which is the default value of `b`, i.e., 1. If there are no default arguments, `__defaults__` will return `None`.
阅读全文