最大尺寸sys模塊的屬性獲取最大值a數據類型的變量Py_ssize_t 可以存儲。它是Python平台的指針那規定Python中列表和字符串的最大大小。 maxsize返回的大小值取決於平台架構:
- 32位:該值將為2^31-1,即2147483647
- 64位:該值將為2^63-1,即9223372036854775807
sys.maxsize
用法: sys.maxsize
Returns:Py_ssize_t的最大值取決於架構
範例1:讓我們在64位係統上獲取最大Py_ssize_t值。
Python3
# importing the module
import sys
# fetching the maximum value
max_val = sys.maxsize
print(max_val)
輸出:
9223372036854775807
範例2:創建具有最大大小的列表。
Python3
# importing the module
import sys
# fetching the maximum value
max_val = sys.maxsize
# creating list with max size
list = range(max_val)
# displaying the length of the list
print(len(list))
print("List succesfully created")
輸出:
9223372036854775807 List succesfully created
範例3:嘗試創建大小大於最大大小的列表。
Python3
# importing the module
import sys
# fetching the maximum value
max_val = sys.maxsize
try:
# creating list with max size + 1
list = range(max_val + 1)
# displaying the length of the list
print(len(list))
print("List succesfully created")
except Exception as e:
# displaying the exception
print(e)
print("List creation unsuccesful")
輸出:
Python int too large to convert to C ssize_t List creation unsuccesful
注:本文由純淨天空篩選整理自Yash_R大神的英文原創作品 sys.maxsize() in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。