用于切片的Python整数无穷大 [英] Python integer infinity for slicing
问题描述
我在配置文件中定义了一个切片参数:
I have defined a slicing parameter in a config file:
max_items = 10
我的班级根据此参数对列表进行切片:
My class slices a list according to this parameter:
items=l[:config.max_itmes]
当 max_items时= 0
,我希望所有项目都来自 l
。快速而肮脏的方式是:
When max_items = 0
, I want all items to be taken from l
. The quick and dirty way is:
config.max_items=config.max_items if config.max_items>0 else 1e7
假设会有少于 1e7
项目。但是,我不喜欢使用魔术数字。是否有更多的Pythonic方法,如无穷大整数常量?
Assuming that there will be less then 1e7
items. However, I don't fancy using magic numbers. Is there a more Pythonic way of doing it, like an infinity integer constant?
推荐答案
没有无穷大整数常量 Python,但在切片中使用无
将导致它使用给定位置的默认值,它们是序列的开头,结尾和每个项目,每个切片的三个部分。
There is no "infinity integer constant" in Python, but using None
in a slice will cause it to use the default for the given position, which are the beginning, the end, and each item in sequence, for each of the three parts of a slice.
>>> 'abc'[:None]
'abc'
这篇关于用于切片的Python整数无穷大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!