Infinite integer in Python

后端 未结 3 1484
耶瑟儿~
耶瑟儿~ 2021-02-11 12:06

Python 3 has float(\'inf\') and Decimal(\'Infinity\') but no int(\'inf\'). So, why a number representing the infinite set of integers is m

3条回答
  •  终归单人心
    2021-02-11 12:38

    For python 2. It is sometimes the case that you need a very large integer. For example, I may want to produce a subarray with x[:n] and, I may wish to sometimes set n to a value such that the whole array will be produced. Since you can't use a float for n (python wants an integer), you need a "large integer". A good way of doing this is to use the largest integer available: sys.maxint. Here is an example:

    # MAX_SOURCES = sys.maxint # normal setting
    MAX_SOURCES = 5 # while testing
    
    # code to use an array ...
    ... sources[:MAX_SOURCES]
    

    So, while testing, I could use a smaller sources array but use the full array in production.

提交回复
热议问题