I assumed sequence types in Python were value types. It turns out they're reference types (Meaning that the value of a variable won't be copied when assigned to a new variable, but referenced). So now I'm wondering, what are the value types in Python? That is, what types in Python can I assign to new variables without worrying that the variable was referenced?
A reference is a name that refers to the specific location in memory of a value (object). References take the form of variables, attributes, and items. In Python, a variable or other reference has no intrinsic type. The object to which a reference is bound at a given time does have a type, however.
If you want to call a list a reference type, then yes, int is a reference type as well, just like every other type in Python.
Python always uses pass-by-reference values. There isn't any exception. Any variable assignment means copying the reference value.
Examples of reference data types are class, Arrays, String, Interface, etc. Examples of primitive data types are int, float, double, Boolean, long, etc.
All values in Python are references. What you need to worry about is if a type is mutable. The basic numeric and string types, as well as tuple
and frozenset
are immutable; names that are bound to an object of one of those types can only be rebound, not mutated.
>>> t = 1, 2, 3 >>> t[1] = 42 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'tuple' object does not support item assignment
Coming from iOS development using strongly typed Swift language, Python reference was a bit confusing so I decided to do a little comparison. Here is the summary: -
a = 10
you are simply pointing/referencing the the object in this case 10
which is stored in a memory. So if that object changes then the value of a variable a
also changes but changing a
does not change the object 10
, This behave similar to Swift Primitive value types such as Int
.To make this clear here is an example: -
# "a" points to an object in this case 10 a = 10 # "b" points to the same object which a points but does not point to a variable a. b = a # Now if we change "a" to point to another object in memory say 20. a = 20 # "b" still points to the old object 10 in other words # "b == 10" but "a == 20", This is because "b" was never pointing to the variable "a" # even though we assigned it as "b = a" instead it was pointing to the object 10 # which is # the same as writing b = 10.
Let's check with a more complex data structure List
list1 = [10,20,30,40] list2 = list1 #[10,20,30,40] list1 = [3,4] # list1 ==> [3,4] # list2 ==> [10,20,30,40]
Again that behave the same to Swift and other similar languages. Here comes the huge difference Let's try changing value at a certain index ( This gets more tricky)
list1 = [10,20,30,40] list2 = list1 #[10,20,30,40] # change value of list 1 at a certain index say index 0 list1[0] = 500 # If you check again the values of list1 and list2 you will be surprised. #list1 ==> [500,20,30,40] #list2 ==> [500,20,30,40]
They both change because they were all pointing to the same object so changing the object changes all list1
and list2
. This is very confusing from other Languages such as Swift. In Swift List/Array are value types meaning they are not referenced instead they are copied around, However in python it is another story, changing a value at a certain index results in changing that value for all properties which references that object just like in the example above. This is very important to keep in mind for folks coming from Swift or other similar languages.
So how do we copy in python?
list1 = [10,20,30,40] list2 = list(list1) # list1 ==> [10,20,30,40] # list2 ==> [10,20,30,40]
Doing so will avoid undesired effects when list1
changes list2
will remain the same.
As an example
list1[0] = 500 #list1 ==> [500,20,30,40] # Changed #list2 ==> [10,20,30,40] # Unchanged
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With