Python教程:sys模块中maxsize()的方法

简介: 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中使用 csv.field_size_limit(sys.maxsize)

在Python中,当我们读取包含巨大字段的CSV文件时,它可能会抛出一个异常,说_csv.Error: field larger than field limit。适当的解决方案是不要跳过一些字段及其行。

要分析CSV,我们需要增加field_size_limit。为此,我们需要实现以下源代码。

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)
相关文章
|
2月前
|
SQL 关系型数据库 数据库
Python SQLAlchemy模块:从入门到实战的数据库操作指南
免费提供Python+PyCharm编程环境,结合SQLAlchemy ORM框架详解数据库开发。涵盖连接配置、模型定义、CRUD操作、事务控制及Alembic迁移工具,以电商订单系统为例,深入讲解高并发场景下的性能优化与最佳实践,助你高效构建数据驱动应用。
352 7
|
2月前
|
监控 安全 程序员
Python日志模块配置:从print到logging的优雅升级指南
从 `print` 到 `logging` 是 Python 开发的必经之路。`print` 调试简单却难维护,日志混乱、无法分级、缺乏上下文;而 `logging` 支持级别控制、多输出、结构化记录,助力项目可维护性升级。本文详解痛点、优势、迁移方案与最佳实践,助你构建专业日志系统,让程序“有记忆”。
258 0
|
2月前
|
JSON 算法 API
Python中的json模块:从基础到进阶的实用指南
本文深入解析Python内置json模块的使用,涵盖序列化与反序列化核心函数、参数配置、中文处理、自定义对象转换及异常处理,并介绍性能优化与第三方库扩展,助你高效实现JSON数据交互。(238字)
387 4
|
2月前
|
索引 Python
Python 列表切片赋值教程:掌握 “移花接木” 式列表修改技巧
本文通过生动的“嫁接”比喻,讲解Python列表切片赋值操作。切片可修改原列表内容,实现头部、尾部或中间元素替换,支持不等长赋值,灵活实现列表结构更新。
130 1
|
2月前
|
Java 调度 数据库
Python threading模块:多线程编程的实战指南
本文深入讲解Python多线程编程,涵盖threading模块的核心用法:线程创建、生命周期、同步机制(锁、信号量、条件变量)、线程通信(队列)、守护线程与线程池应用。结合实战案例,如多线程下载器,帮助开发者提升程序并发性能,适用于I/O密集型任务处理。
290 0
|
2月前
|
XML JSON 数据处理
超越JSON:Python结构化数据处理模块全解析
本文深入解析Python中12个核心数据处理模块,涵盖csv、pandas、pickle、shelve、struct、configparser、xml、numpy、array、sqlite3和msgpack,覆盖表格处理、序列化、配置管理、科学计算等六大场景,结合真实案例与决策树,助你高效应对各类数据挑战。(238字)
204 0
|
SQL JSON C语言
Python中字符串的三种定义方法
Python中字符串的三种定义方法
563 2
|
Python
Python面向对象、类的抽象、类的定义、类名遵循大驼峰的命名规范创建对象、类外部添加和获取对象属性、类内部操作属性魔法方法__init__()__str__()__del__()__repr__()
面向对象和面向过程,是两种编程思想. 编程思想是指对待同一个问题,解决问题的套路方式.面向过程: 注重的过程,实现的细节.亲力亲为.面向对象: 关注的是结果, 偷懒.类和对象,是面向对象中非常重要的两个概念object 是所有的类基类,即最初始的类class 类名(object): 类中的代码PEP8代码规范:类定义的前后,需要两个空行 创建的对象地址值都不一样如dog和dog1的地址就不一样,dog的地址为2378043254528dog1的地址为2378044849840 8.类内部操作属性 sel
476 1
Python面向对象、类的抽象、类的定义、类名遵循大驼峰的命名规范创建对象、类外部添加和获取对象属性、类内部操作属性魔法方法__init__()__str__()__del__()__repr__()
28.从入门到精通:Python3 面向对象 面向对象技术简介 类定义 类对象 类的方法
28.从入门到精通:Python3 面向对象 面向对象技术简介 类定义 类对象 类的方法

推荐镜像

更多