函数形参中的*的作用及用法,求大佬解答
求大佬解答,问题在下面:这是库函数的内容,
1、不理解函数中*代表的意义,他后面也没有跟变量名
2、返回函数中func的参数哪里获取到
def api_route(
self,
path: str,
*,
response_model: Optional] = None,
status_code: Optional = None,
tags: Optional] = None,
dependencies: Optional] = None,
summary: Optional = None,
description: Optional = None,
response_description: str = "Successful Response",
responses: Optional, Dict]] = None,
deprecated: Optional = None,
methods: Optional] = None,
operation_id: Optional = None,
response_model_include: Optional] = None,
response_model_exclude: Optional] = None,
response_model_by_alias: bool = True,
response_model_exclude_unset: bool = False,
response_model_exclude_defaults: bool = False,
response_model_exclude_none: bool = False,
include_in_schema: bool = True,
response_class: Type = Default(JSONResponse),
name: Optional = None,
callbacks: Optional] = None,
openapi_extra: Optional] = None,
) -> Callable[, DecoratedCallable]:
def decorator(func: DecoratedCallable) -> DecoratedCallable:
self.add_api_route(
path,
func,
response_model=response_model,
status_code=status_code,
tags=tags,
dependencies=dependencies,
summary=summary,
description=description,
response_description=response_description,
responses=responses,
deprecated=deprecated,
methods=methods,
operation_id=operation_id,
response_model_include=response_model_include,
response_model_exclude=response_model_exclude,
response_model_by_alias=response_model_by_alias,
response_model_exclude_unset=response_model_exclude_unset,
response_model_exclude_defaults=response_model_exclude_defaults,
response_model_exclude_none=response_model_exclude_none,
include_in_schema=include_in_schema,
response_class=response_class,
name=name,
callbacks=callbacks,
openapi_extra=openapi_extra,
)
return func
return decorator 第二个问题想明白了,函数本身返回内部函数名add.api_route作为装饰器修饰func函数
第一个问题还是不明白,百度上也没找到,自己试了下,可以通过,但是不理解,没有参数名,如何使用
星号本身并不是一个参数,而是:
*之后的所有参数都只能以关键参数的形式进行传值,不接收其他任何形式的传值。 https://fishc.com.cn/thread-133622-1-1.html
打包参数(pack)和拆分参数(unpack)打包参数(pack)和拆分参数(unpack) wp231957 发表于 2022-1-5 07:57
星号本身并不是一个参数,而是:
*之后的所有参数都只能以关键参数的形式进行传值,不接收其他任何形式的 ...
正解,就是关键字参数打漏了一个“字”
页:
[1]