python中char是什么_python有char类型吗
python没有char类型,一个字符也是字符串。
为什么在Python中没有专门的char数据类型呢?
简单胜于复杂。在 Python 中, 字符串中的每个字符占的空间大小是 8 bit.>>> import sys
>>> sys.getsizeof('')
37
>>> sys.getsizeof('a')
38
可以看到, 空字符占用37个 byte, 长度为1的字符串 'a' 占内存 38个 byte. 多了一个字符 a 之后多了 1 个 byte.
在 Python 内部, 字符串是这样实现的typedef struct {
PyObject_VAR_HEAD
long ob_shash;
int ob_sstate;
char ob_sval[1];
/* Invariants:
* ob_sval contains space for 'ob_size+1' elements.
* ob_sval[ob_size] == 0.
* ob_shash is the hash of the string or -1 if not computed yet.
* ob_sstate != 0 iff the string object is in stringobject.c's
* 'interned' dictionary; in this case the two references
* from 'interned' to this object are *not counted* in ob_refcnt.
*/
} PyStringObject;
每个 char 就是存在 ob_sval 里面的, 占大小 8bit. 余下的36个 byte 主要来自于宏 PyObject_VAR_HEAD. 实际上 python 的string实现还用到了一个叫 *interned 的全局变量, 里面可以存长度为 0 或 1 的字符串, 也就是 char, 可以节省空间并且加快速度./* This dictionary holds all interned strings. Note that references to
strings in this dictionary are *not* counted in the string's ob_refcnt.
When the interned string reaches a refcnt of 0 the string deallocation
function will delete the reference from this dictionary.
Another way to look at this is that to say that the actual reference
count of a string is: s->ob_refcnt + (s->ob_sstate?2:0)
*/
static PyObject *interned;
实际上在 python 里既没有指针也没有"裸露的数据结构" (非对象), 连最简单的整数 integer 都是这样实现的typedef struct {
PyObject_HEAD
long ob_ival;
} PyIntObject;
总而言之, 这样的设计满足 python 的 "一切都是对象", "一切都尽可能simple" 的设计思想。
python中char是什么_python有char类型吗相关推荐
- python中sn的意思_python学习笔记
学习资料:<简明 Python 教程.pdf>Python是一种简单易学,功能强大的编程语言,它有高效率的高层数据结构,简单而有效地实现面向对象编程. 特点:1.简单 2.易学 3.开源, ...
- python中lis的意思_Python语言入门(二)
上篇文章中,我们初步熟悉了print()函数,字符串以及多行字符串.这篇文章,我们将熟悉其它的数据类型,变量,列表以及基本运算.先讲讲数据类型. 1.int(整形).理论上,32位系统的int数据最多 ...
- python中byte2array报错_python基础-bytes和bytearray的用法
Python中的序列类型有bytes和bytearray. 二进制序列类型的用法比较少见,是python中少用的一种序列类型,对于二进制序列类型,大家基本了解即可. bytes二进制序列类型 指定长度 ...
- python中for循环流程图_Python流程控制-while循环-for循环
写重复代码 是可耻的行为 -------------- 完美的分割线 -------------- 程序在一般情况下是按顺序执行的,编程语言提供了各种控制结构,允许更复杂的执行路径. 循环(loop) ...
- 为什么python中不需要变量名和变量类型声明?
python是解释型语言,python的变量不需要声明,但是需要初始化,python解释器会为每个出现的对象分配内存,哪怕他们的值完全相等(注意是相等不是相同),其中变量其实通过一个标记调用内存中的值 ...
- python 测试字符串类型_【教程】如何用Python中的chardet去检测字符编码类型
[背景] 之前已经使用过chardet了,也算用了不少次了. 之前也写过和chardet相关的: 但是没写教程,举例说明如何使用. 现在去举例解释解释. [python示例代码演示如何用chardet ...
- python鸭制作类代码_python之鸭子类型
python不支持多态,也不用支持多态,python是一种多态语言,崇尚鸭子类型. 在程序设计中,鸭子类型是动态类型的一种风格,不是由继承特定的类或实现特定的接口,而是当前的方法和属性的集合决定,鸭子 ...
- Python中 redis StrictRedis对象操作string类型
Python中 redis StrictRedis对象操作string类型 准备 在桌面上创建redis目录 使用pycharm打开 redis目录 创建redis_string.py文件 from ...
- python中为什么不支持char_python支持char吗
Python 之禅第三条 Simple is better than complex. 在 Python 中, string 中的每个字符占的空间大小是 8 bit. >>> imp ...
最新文章
- column 对齐排列命令
- html中after伪类原理,css伪类before跟after原理与使用(原)
- 2020-12-16(虚析构函数,神逻辑代码)
- Laravel5.2目录结构及composer.json文件解析
- html图片分开,webpack单独分离打包css,css里引用的图片路径错误,怎么解决?
- TurtleCoin节点搭建
- 剑指Offer值二叉树的深度
- python3,判断,循环练习1
- 命令行基础 挂载 文件目录管理 vim编辑器
- echarts制作航线轨迹原来是这样的
- Hadoop2.7.1+Hbase1.2.1集群环境搭建(3)1.x和2.x都支持的集群安装方式
- 13个搭讪绝招助你告别单身
- 小工具:找出序列中的极值点
- Java并发之魂——synchronized深度解析
- 源码免杀教程 源码免杀思路详解
- 宝塔面板部署nuxt项目线程守护启动以及Nginx反向代理
- 数字图像位移传感器在大坝安全监测运用
- usb相关资料整理(四)
- mysql tgz 安装_mysql的安装
- 如何快速理解IMU积分、预积分、误差、方差