栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Python

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python

什么?浮游物是一成不变的吗?但是我不能

x = 5.0x += 7.0print x # 12.0

那不是

“ mut” x
吗?

好吧,你同意字符串是不可变的,对吗?但是你可以做同样的事情。

s = 'foo's += 'bar'print s # foobar

变量的值会更改,但是会通过更改变量引用的内容来更改。一个可变的类型可以改变这种方式,它可也 “到位”而改变。

这是区别。

x = something # immutable typeprint xfunc(x)print x # prints the same thingx = something # mutable typeprint xfunc(x)print x # might print something differentx = something # immutable typey = xprint x# some statement that operates on yprint x # prints the same thingx = something # mutable typey = xprint x# some statement that operates on yprint x # might print something different

具体例子

x = 'foo'y = xprint x # fooy += 'bar'print x # foox = [1, 2, 3]y = xprint x # [1, 2, 3]y += [3, 2, 1]print x # [1, 2, 3, 3, 2, 1]def func(val):    val += 'bar'x = 'foo'print x # foofunc(x)print x # foodef func(val):    val += [3, 2, 1]x = [1, 2, 3]print x # [1, 2, 3]func(x)print x # [1, 2, 3, 3, 2, 1]


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/371080.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号