设为首页 加入收藏

TOP

Python教程:sys模块中maxsize()的方法(一)
2023-09-09 10:25:44 】 浏览:163
Tags:Python 教程 sys maxsize 方法

在Python中,sys模块有一个名为maxsize()的方法。这个方法返回一个变量Py_ssize_t可以容纳的最大值。

Py_ssize_t是一个整数,它给出了变量可以取的最大值。大小因操作系统的位而异。

32位的大小为(2 power 31)-1,64位的大小为(2 power 63)-1。

sys.maxsize 方法

sys.maxsize()

返回:此方法根据平台类型返回最大大小值Py_ssize_t。

代码1:使用 sys.maxsize() 方法

要实现方法sys.maxsize()并检查最大大小值,我们可以导入sys模块并使用方法maxsize()。根据平台架构类型,sys.maxsize()方法在控制台上返回其最大值大小。

下面是32位和64位操作系统的实现,并运行相同的sys.maxsize()方法。

32-Bit平台

# 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平台

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的大小。

最大尺寸

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

大于最大大小

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的更好理解。

属性错误

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 执行

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中使用 c

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Python 创建或读取 Excel 文件 下一篇Python 基础面试第四弹

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目