Python integer infinity for slicing

喜你入骨 提交于 2019-12-08 15:48:53

问题


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]

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

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?


回答1:


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'



回答2:


Have you tried with sys.maxint?



来源:https://stackoverflow.com/questions/6723009/python-integer-infinity-for-slicing

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