In any python API documentation, why are optional arguments written in a nested style?

夙愿已清 提交于 2019-12-11 02:41:17

问题


It's just, why do this:

find([spec=None[, fields=None[, skip=0[, limit=0[, timeout=True[, snapshot=False[, tailable=False[, sort=None[, max_scan=None[, as_class=None[, slave_okay=False[, await_data=False[, partial=False[, manipulate=True[, read_preference=ReadPreference.PRIMARY[, exhaust=False[, compile_re=True[, **kwargs]]]]]]]]]]]]]]]]]])

Which seems ugly and confusing, yet appears in every api docs I've ever come across, I may lack some basic python knowledge.


回答1:


I do not have any authentic source to back my statement

Optional positional arguments presented in a nested style would enforce the proper ordering of the fields.

For Example in os module for fdopen

os.fdopen(fd[, mode[, bufsize]])

Indicates that mode and bufsize are optional but if you are specifying bufsize you should also specify mode.

For Keywords argument, on the other hand, a default value is specified without any order enforcement to inndicate that the value is optional in which case, the parameter would be initialized with the default value

For example in [re] module for split

split(string, maxsplit=0)



回答2:


This is a convention borrowed from extended Backus-Naur form. As pointed out above by Abhijit, the nesting means optional to the optional argument etc.



来源:https://stackoverflow.com/questions/24950651/in-any-python-api-documentation-why-are-optional-arguments-written-in-a-nested

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!