sys.maxsize 方法
在Python中,sys模块有一个名为maxsize()的方法。这个方法返回一个变量Py_ssize_t可以容纳的最大值。
Py_ssize_t是一个整数,它给出了变量可以取的最大值。大小因操作系统的位而异。
32位的大小为(2 power 31)-1,64位的大小为(2 power 63)-1。
1 | sys.maxsize() |
返回:此方法根据平台类型返回最大大小值Py_ssize_t。
代码1:使用 sys.maxsize() 方法
要实现方法sys.maxsize()并检查最大大小值,我们可以导入sys模块并使用方法maxsize()。根据平台架构类型,sys.maxsize()方法在控制台上返回其最大值大小。
下面是32位和64位操作系统的实现,并运行相同的sys.maxsize()方法。
32-Bit平台
1 2 3 4 5 6 7 | # import the sys module to use the maxsize() method import sys # returns the maximum size size = sys.maxsize print ( "The maximum size of a 32-bit platform is:" , size) #输出: The maximum size of a 32 - bit platform is : 2147483647 |
64-Bit平台
1 2 3 4 5 6 | import sys # returns the maximum size size = sys.maxsize print ( "The maximum size of a 32-bit platform is:" , size) #输出: The maximum size of a 64 - bit platform is : 9223372036854775807 |
代码2:检查列表的最大大小 sys.maxsize() 方法
为了检查我们系统的最大大小,我们可以使用range()方法来传递列表的最大大小,然后检查它的长度。类似地,在第二个例子中,我们超过了最大大小,Python解释器捕获了异常并返回int too large to convert to C ssize_t错误。
在下面的例子中,我们可以观察到对Py_ssize_t设置限制的效果。不可能索引一个元素大于其大小的列表,因为它不接受非Py_ssize_t。
关于字典数据结构,Py_ssize_t使用哈希,因为Python没有使用LinkedList来实现它。类似地,字典中的大小不能大于Py_ssize_t的大小。
最大尺寸
1 2 3 4 5 6 7 8 9 10 11 | import sys size = sys.maxsize # creates the max length list = range (size) # returns the length of a list print ( "The maximum length of a list:" , len ( list )) print ( "List is created successfully" ) #输出: # maximum size limit on a 64-bit platform The maximum length of a list : 9223372036854775807 List is created successfully |
大于最大大小
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import sys size = sys.maxsize # handles the exception try : # creates a list with maximum size + 1 list = range (size + 1 ) # check the maximum size print ( len ( list )) print ( "List is created successfully" ) # exception if the size goes beyond the maximum size except Exception as exception: print ( "Exception caught: " , exception) print ( "List is not created due to above exception" ) #Python小白学习交流群:711312441 #输出: # output shows exception occurs Exception caught: Python int too large to convert to C ssize_t List is not created due to above exception |
代码3:该 sys.maxsize() 对比 sys.maxint 方法
sys.maxint()方法不再支持Python 3作为整数。如果我们使用这个方法或常量,我们将得到下面的AttributeError: module ‘sys’ has no attribute ‘maxint’。
为了在Python 3.0中克服这个问题,引入了另一个常量sys.maxsize,我们知道它会返回Py_ssize_t的最大值。在Python 3中,int和long int是合并的。
第一个实现展示了AttributeError的示例,第二个源代码揭示了对maxint的更好理解。
属性错误
1 2 3 4 5 6 7 8 9 10 | import sys li = [ 20 , 2 , 23 , 88 , 3 , 63 , 12 ] # sys.maxint is not supported in python 3. We need to use python version < 3.0 min_value = sys.maxint for i in range ( 0 , len (li)): if li[i] < min_value: min_value = li[i] print ( "Min value : " + str (min_value)) 输出: AttributeError: module 'sys' has no attribute 'maxint' |
maxint 执行
1 2 3 4 5 6 7 8 9 10 11 | import sys max_int = sys.maxsize min_int = sys.maxsize - 1 long_int = sys.maxsize + 1 print ( "Maximum integer size is : " + str (max_int) + " , " + str ( type (max_int))) print ( "Maximum integer size-1 is :" + str (max_int) + " , " + str ( type (min_int))) print ( "Maximum integer size+1 is :" + str (max_int) + " , " + str ( type (long_int))) #输出: Maximum integer size is : 9223372036854775807 , < class 'int' > Maximum integer size - 1 is : 9223372036854775807 , < class 'int' > Maximum integer size + 1 is : 9223372036854775807 , < class 'int' > |
代码4:在Python中使用 csv.field_size_limit(sys.maxsize)
在Python中,当我们读取包含巨大字段的CSV文件时,它可能会抛出一个异常,说_csv.Error: field larger than field limit。适当的解决方案是不要跳过一些字段及其行。
要分析CSV,我们需要增加field_size_limit。为此,我们需要实现以下源代码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import sys # to use the field_size_limit method import csv maximum_Integer = sys.maxsize while True : # read the csv with huge fields with open ( 'myfile.csv' , newline = '') as f: reader = csv.reader(f) for row in reader: print (row) # Here, we reduce the size if there is an overflow error try : csv.field_size_limit(maximum_Integer) break except OverflowError: maximum_Integer = int (maximum_Integer / 10 ) |
以上就是Python sys模块中maxsize()方法教程示例的详细内容,更多关于Python sys模块maxsize方法的资料请关注脚本之家其它相关文章!
最新评论