[13]integer immutability,refcount #python (deep)lesson#1

See gc.get_referrers() and sys.getrefcount() …
Do you know — list Objects are mutable; integer Objects are Immutable? In java, c#, c, int object is Mutable.
—-Now look at integer objects, which are Immutable
i=2 # rebinding
i=1 # rebinding back to original object!

j=i # ref count = 2 on the shared object


—-Now look at lists. Do lists hold items by value or reference? More specifically,
– Add() by pbref? yes according to many posts in this blog and also [[python essential ref]]
– Read() by pbref? Yes. pbclone would make myList[0].edit() meaningless.
– qq(mylist[2] = ….) is by reference? Probably yes, otherwise counter-intuitive.

How do you verify? How about id(mylist[0]). Note

id( i )
arr.append(i) # pbref. Ref count = 2
id( arr[0] )
i=2 # Rebinding! not “content edit”. i now points to a new object, so ref count = 1 on the original object! Counter-intuitive to me.
arr[0] = 2.1 # Rebinding, not “content edit”. arr[0] now points to a new object, so ref count = 0 on the original object “1.9”
id( arr[0] )

Q: is it possible to change the value of arr[0]?
A: arr[0] currently points to an int object. We can rebind this pointer to a “9.89” object. However, if you want to change pointee object content at arr[0], then it depends on the pointee object type. For integer objects, content is immutable, so answer is NOWAY.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s