首页 > 编程语言 >Why are Python strings immutable? 字符串是否可以改变

Why are Python strings immutable? 字符串是否可以改变

时间:2023-05-11 21:02:12浏览次数:42  
标签:typesinternal Python immutable UnassignableOperand 字符串 strings

实践

1、

python

s="abc" s+="34"  # OK  print(s) s[0]="k"  # TypeError: 'str' object does not support item assignment    

 golang

    s := "abc"     s += "456"     fmt.Println(s)     s[0] = "A" // cannot assign to s[0] (value of type byte)compiler UnassignableOperand       typesinternal package - golang.org/x/tools/internal/typesinternal - Go Packages https://pkg.go.dev/golang.org/x/tools/internal/typesinternal#UnassignableOperand
	// UnassignableOperand occurs when the left-hand side of an assignment is
	// not assignable.
	//
	// Example:
	//  func f() {
	//  	const c = 1
	//  	c = 2
	//  }
	UnassignableOperand
   

 

 

 

Design and History FAQ — Python 3.11.3 documentation https://docs.python.org/3/faq/design.html#why-are-python-strings-immutable

 

为什么Python字符串是不可变的?

有几个优点。

一个是性能:知道字符串是不可变的,意味着我们可以在创建时为它分配空间,并且存储需求是固定不变的。这也是元组和列表之间区别的原因之一。

另一个优点是,Python 中的字符串被视为与数字一样“基本”。 任何动作都不会将值 8 更改为其他值,在 Python 中,任何动作都不会将字符串 "8" 更改为其他值。

 

Why are Python strings immutable?

There are several advantages.

One is performance: knowing that a string is immutable means we can allocate space for it at creation time, and the storage requirements are fixed and unchanging. This is also one of the reasons for the distinction between tuples and lists.

Another advantage is that strings in Python are considered as “elemental” as numbers. No amount of activity will change the value 8 to anything else, and in Python, no amount of activity will change the string “eight” to anything else.

 

 

 

 

 

翻译

搜索

复制

标签:typesinternal,Python,immutable,UnassignableOperand,字符串,strings
From: https://www.cnblogs.com/papering/p/17392210.html

相关文章

  • python环境的安装与设置和oneforall的安装与使用
    下载python:https://www.python.org/downloads/windows/安装python如果不需要修改路径,下面两个√打开后,点击上面的installnow也可以可以选择修改安装路径下载OneForALL:在github上边下载安装OneForALL复制你安装OneForALL的路径,比如我的是C:Windows\OneForAll-master回到桌面,按win......
  • 使用Open3D进行PCD拟合平面的Python代码示例
    使用Open3D进行PCD拟合平面的Python代码示例 importopen3daso3dimportnumpyasnp#读取点云数据pcd=o3d.io.read_point_cloud("2023042501.pcd")#创建PCD图pcd_graph=o3d.geometry.PointCloudGraph(pcd)#选择要拟合的平面plane_cent......
  • Python协程asyncio
    在Python使用multiprocessing进行多线程和多进程操作 这篇文章中介绍了使用多线程的方式对一些I/O操作(文件读写、网络请求,这些操作不用等待其结束,在此期间可以做其他事情)进行加速。而本篇文章介绍的协程可以理解成“微线程”,不开辟其他线程,只在一个线程中执行,并且执行函数时......
  • Python OOP & Class private method All In One
    PythonOOP&ClassprivatemethodAllInOnePythonClassprivatemethoddemos代码缩进错误调用私有方法错误#!/usr/bin/python3#类定义classpeople:#定义基本属性name=''age=0#定义私有属性,私有属性在类外部无法直接进行访问_......
  • Python range function All In One
    PythonrangefunctionAllInOnerange函数函数语法range(stop)range(start,stop[,step])参数说明:start:计数从start开始。默认是从0开始。例如range(5)等价于range(0,5)stop:计数到stop结束,但不包括stop。例如:range(0,5)是[0,1,2,3,4]没有......
  • Python的基础核心知识
    编程语言和编程编程语言语言:人与人之间沟通的媒介编程语言:人与计算机沟通的语言编程程序员通过计算机能够读懂的语言把自己的思想和逻辑写下来的过程编程的初衷是更好的奴隶计算机计算机五大组成部分部1.控制器2.运算器3.存储器4.输出设备5.输入设备计算机三大核心硬......
  • 初学计算机python
    今天正式开始学习计算机python,从最基础的概念开始。学习计算机需要使用一款编辑软件辅助学习,Typora是个很好的选择。初学Typora目前只对一些基本快捷键做了解,做前期准备。对六种标题快捷键,几个#就是几级标题,或者直接Ctrl+1、2、3、4、5、6转变相应等级标题。另外还有无序标题:“......
  • python异步正则字符串替换,asyncio异步正则字符串替换re
     自然语言处理经常使用re正则模块进行字符串替换,但是文本数量特别大的时候,需要跑很久,这就需要使用asyncio异步加速处理importpandasaspdimportreimportasynciodata=pd.read_csv("guba_all_post_20230413.csv")data.dropna(inplace=True)#defreplace_betwee......
  • python异步字符串查找,asyncio和marisa_trie
     自然语言处理当中经常需要字符串的查找操作,比如通过查找返回字串在文本当中的位置,比如通过匹配实现的nerimportpandasaspdimportasyncio#data=pd.read_csv("guba_fc_result_20230413.csv")data=pd.read_csv("guba_all_post_20230413.csv")filename="cate_gr......
  • R语言中调用调用python
       ......