James Gosling:From a strategic point of view, they tend to more often be trouble free. And there are usually things you can do with immutables that you can't do with mutable things, such as cache the result. If you pass a string to a file open method, or if you pass a string to...
>>> floatA = 3.14 # float values are immutable >>> dictA = {tupleA : True, stringA : False, floatA : True} # no error as all keys are immutable >>> print(dictA) {(1, 2, 3): True, 'I love Python!': False, 3.14: True} >>> listB = [1, 2, 3] #list is mutable >...
Similar optimization applies to other immutable objects like empty tuples as well. Since lists are mutable, that's why [] is [] will return False and () is () will return True. This explains our second snippet. Let's move on to the third one,...
whereas lists are sequences of any type of Python objects. 字符串和列表之间的另一个区别是字符串是不可变的,而列表是可变的。 Another difference between strings and lists is that strings are immutable, whereas lists are mutable. 除了这两个区别之外,字符串和列表当然也有自己的方法。 In addition to...
In practice, what that means is you can use sets for immutable objects like numbers and strings, but not for mutable objects like lists and dictionaries. 有两种类型的集合。 There are two types of sets. 一种类型的集合称为“集合”。 One type of set is called just "a set". 另一种类型...
可更改(mutable)与不可更改(immutable)对象 在python 中,strings, tuples, 和 numbers 是不可更改的对象,而 list,dict 等则是可以修改的对象。 不可变类型:变量赋值 a=5 后再赋值 a=10,这里实际是新生成一个 int 值对象 10,再让 a 指向它,而 5 被丢弃,不是改变a的值,相当于新生成了a。
---> 1 hash((1, 2, [2, 3])) # fails because lists are mutable TypeError: unhashable type: 'list' 要用列表当做键,一种方法是将列表转化为元组,只要内部元素可以被哈希,它也就可以被哈希: In [130]: d = {} In [131]: d[tuple([1, 2, 3])] = 5 In...
3.1 可更改(mutable)与不可更改(immutable)对象 3,2 必备参数 3.3 关键字参数 3.4 默认参数 3.5 不定长参数 4. 变量作用域 函数使用 参数 作用域 year = int(input("请输入一个年份:")) if (year % 4) == 0 and (year % 100) != 0 or (year % 400) == 0: ...
Strings have very specific limitations on what type of data they can store, and that is Unicode text.bytes and its mutable alternative (bytearray) differs from str by allowing only bytes as a sequence value—integers in the range 0 <= x < 256. This may be confusing at the beginning, ...
aliasing has a possibly surprising effect on the semantics of Python code involving mutable objects ...