|
In situ type variable and immutable types
Situ immutable type can be called a hash (hashable) type, also known as in situ variable type is not available hash type.
Situ immutable type:
Numeric types: int, float, decimal.Decimal, fractions.Fraction, complex
String type: str, bytes
tuple
frozenset
Boolean type: True, False
None
Situ variable type:
list
dict
set
How to actually test whether a variable place
hash or will return to their home can not change the type of hash value, if a second call to place variable type function, then returns TypeError.
Only hash types can only be used as dict key.
Only the type of hash can only be placed in the set, so the set itself is not stored in a set of nested.
for example:
>>> Hash (b'aaa ')
6904179387427091653
>>> Hash (bytearray (b'aaa '))
Traceback (most recent call last):
File "< stdin>", line 1, in < module>
TypeError: unhashable type: 'bytearray'
>>> Hash (frozenset ({1,2,3}))
-7,699,079,583,225,461,316
>>> Hash ({1,2,3})
Traceback (most recent call last):
File "< stdin>", line 1, in < module>
TypeError: unhashable type: 'set'
How to verify whether the operation is a practical place changed.
Built-in function id () returns the address of an object actually stored in memory.
>>> L = [1]
>>> Id (L)
49689480
>>> L.append (2)
>>> Id (L)
49689480 # in-place editing, so you can see the store address has not changed.
>>> S = 'aa'
>>> Id (s)
47072456
>>> S + = 'bb'
>>> Id (s)
# 49700008 not in place due to the changes, so when the string changes, opened up a new store a memory address.
>>>
Data stored in an orderly manner
Ordered data type
Ordered data type called sequence, support indexing, slicing, addition, multiplication, calculate the length, compare the size of the operation.
Comparison will compare the size of each element in turn, if not followed when comparing the same type found will pop up an error.
list
tuple
str
bytes
Unordered data type
dict
set |
|
|
|