可變和不可變

2021-10-04 20:35:52 字數 1048 閱讀 4427

#可變和不可變

#不可變:物件所指向的記憶體中的值是不可以改變

#不可變型別:int ser float 元組tuple

num=

10s1=

'abc'

print(id

(s1)

)s1=

'abcd'

print(id

(s1)

)t1=(2

,5,6

)print(id

(t1)

)t1=(2

,5)print(id

(t1)

)#可變的:物件所指向的記憶體中的值是可以改變

#可變型別:字典dict 列表 list

list1=[1

,2,4

,5,5

]print

(list1,

id(list1)

)list1.pop(

)print

(list1,

id(list1)

)dict1=

print

(dict1,

id(dict1)

)dict1.pop(1)

print

(dict1,

id(dict1)

)'''

集合是可變的還是不可以變的

'''set

=print

(set,id

(set))

set.add(2)

print

(set,id

(set

))

執行結果:

2421760

2469664

3148736

2478768[1

,2,4

,5,5

]2110968[1

,2,4

,5]2110968

2452096

2452096

3124440

3124440

可變物件和不可變物件

要理解可變物件和不可變物件,先要理解final關鍵字。參考此部落格 沒有經過原博主的同意便引用了一些,侵刪!final關鍵字可以用來修飾類 方法和變數 成員或區域性 final修飾類時表明這個類不能被繼承。final類中的方法會被隱式的定義為fianl,變數自行定義需要不要被final修飾。fina...

18 可變和不可變集合

對可變集合中資料的增刪改之後會返回乙個新的集合 def main args array string unit object scala02array println s array 1 for s array array.foreach s println s s foreach表示對array中...

可變物件和不可變物件

python 在堆 heap 中分配的物件分為兩類,可變物件和不可變物件 物件的內容發生變化時,變數的物件引用是不會變化的 dict print s d dict,id dict dict a4 4 print s d dict,id dict output 42838752 42838752 不可...