A slash in the argument list of a function denotes that the parameters prior to it are positional-only. Let us first see a function in Python with a parameter −
Function in Python
Example
在这里,我们正在使用参数myStr在Python中创建一个基本函数 -
# Creating a Function def demo(myStr): print("Car =: ",myStr) # function call demo("BMW") demo("Tesla")
输出
Car =: BMW Car =: Tesla
Slash in the parameter list of a function
如上所述,函数参数列表中的斜杠表示其之前的参数是仅位置参数。
在调用接受仅位置参数的函数时,参数将仅根据它们的位置进行映射。
divmode()函数
divmod() 函数是一个函数列表中斜杠的完美示例,即它接受位置参数,如下所示 −
divmod(a, b, /)
上面,由于斜杠位于参数列表的末尾,参数a和b都是位置参数。
Let us print the documentation of divmod() using the help() functiojn in Python
# Creating a Function def demo(myStr): print(help(divmod)) # function call demo("BMW") demo("Tesla")
输出
Help on built-in function divmod in module builtins: divmod(x, y, /) Return the tuple (x//y, x%y). Invariant: div*y + mod == x. None
Now, let us see an example of the divmod(). Both the parameters are dividend and divisor −
k = divmod(5, 2) print(k)
输出
(2, 1)
参数列表末尾的斜杠表示两个参数都是位置参数。因此,如果我们使用关键字参数调用divmod(),将会引发错误 −
divmod(a = 5, b = 2)
输出
In the above example, an error occurred since the divmod() takes no keyword arguments.
以上就是在Python中,函数参数列表中的斜杠(/)表示分隔位置参数和关键字参数的界限的详细内容,更多请关注编程之家其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。