简体   繁体   中英

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]

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?

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'

Have you tried with sys.maxint ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM