如何实现带有签名的python方法,例如[[start,] stop [,step]),即左侧的默认关键字参数 [英] How to implement python method with signature like ([start ,] stop [, step]), i.e. default keyword argument on the left
问题描述
由于在python 3.x中,build-id range()
函数不再返回列表,而是返回可迭代列表,因此某些旧代码失败,因为我使用range()
方便地生成了所需的列表.
Since in python 3.X the build-id range()
function returns no longer a list but an iterable, some old code fails as I use range()
to conveniently generate lists I need.
因此,我尝试像这样实现自己的lrange
函数:
So I try to implement my own lrange
function like this:
def lrange(start = 0, stop, step = 1):
ret = []
while start < stop:
ret.append(start)
start += step
return ret
给我一个非默认参数跟随默认参数"的解释器错误.
giving me a "non-default argument follows default argument" interpreter error.
如果我看一下Python的 range(),这似乎是有可能的
If I look at Python's range() it seems to be possible.
我发布这个问题主要是因为我想知道是否/如何可以自己实现带有这种签名的功能
I posted this question mainly because I was wondering if/how one can implement a function with such a signature on his own
推荐答案
快速解答
当我第一次开始学习Python时,这个问题突然出现,我认为值得在这里记录该方法.仅使用一项检查来模拟原始行为.
Quick Answer
This question popped up when I first started learning Python, and I think it worthwhile to document the method here. Only one check is used to simulate original behavior.
def list_range(start, stop=None, step=1):
if stop is None:
start, stop = 0, start
return list(range(start, stop, step))
我认为该解决方案比使用所有关键字参数或*args
更为优雅.
I think this solution is a bit more elegant than using all keyword arguments or *args
.
使用前哨
正确实现这一目标的关键是使用哨兵对象来确定是否获得第二个参数,如果没有,则在将第一个参数移至第二个参数时为第一个参数提供默认值.
The key to getting this right is to use a sentinel object to determine if you get a second argument, and if not, to provide the default to the first argument while moving the first argument to the second.
None
是Python的空值,是一种很好的最佳做法标记,而惯用的检查方法是使用关键字is
,因为它是单例.
None
, being Python's null value, is a good best-practice sentinel, and the idiomatic way to check for it is with the keyword is
, since it is a singleton.
带有适当文档字符串的示例,声明了签名/API
def list_range(start, stop=None, step=1):
'''
list_range(stop)
list_range(start, stop, step)
return list of integers from start (default 0) to stop,
incrementing by step (default 1).
'''
if stop is None:
start, stop = 0, start
return list(range(start, stop, step))
演示
>>> list_range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list_range(5, 10)
[5, 6, 7, 8, 9]
>>> list_range(2, 10, 2)
[2, 4, 6, 8]
如果不提供任何参数,则会引发错误,这与此处的全关键字解决方案不同.
And it raises an error if no arguments are given, unlike the all-keyword solution here.
顺便说一句,我希望读者仅从理论角度考虑这一点,除非在中央规范位置使用该功能以使代码在Python 2和Linux之间相互兼容,否则我认为该功能不值得维护. 3.在Python中,使用内置函数将范围具体化为列表非常简单:
By the way, I hope this is only considered from a theoretical perspective by the reader, I don't believe this function is worth the maintenance, unless used in a central canonical location to make code cross-compatible between Python 2 and 3. In Python, it's quite simple to materialize a range into a list with the built-in functions:
Python 3.3.1 (default, Sep 25 2013, 19:29:01)
[GCC 4.7.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
这篇关于如何实现带有签名的python方法,例如[[start,] stop [,step]),即左侧的默认关键字参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!