0% found this document useful (0 votes)
6 views

python3

The document explains reference semantics in programming, particularly how assignment manipulates references rather than copying objects. It highlights the difference between mutable and immutable data types, illustrating how changes to mutable objects affect all references to them. Examples demonstrate how variable assignments and modifications work, emphasizing the importance of understanding references to avoid unexpected behavior in code.

Uploaded by

Ross Lyngdoh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

python3

The document explains reference semantics in programming, particularly how assignment manipulates references rather than copying objects. It highlights the difference between mutable and immutable data types, illustrating how changes to mutable objects affect all references to them. Examples demonstrate how variable assignments and modifications work, emphasizing the importance of understanding references to avoid unexpected behavior in code.

Uploaded by

Ross Lyngdoh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 17

Reference semantics,

variables and names


Understanding Reference Semantics
 Assignment manipulates references
—x = y does not make a copy of the object y references
—x = y makes x reference the object y references
 Very useful; but beware!, e.g.
>>> a = [1, 2, 3] # a now references the list [1, 2, 3]
>>> b = a # b now references what a references
>>> a.append(4) # this changes the list a
references
>>> print b # if we print what b references,
[1, 2, 3, 4] # SURPRISE! It has changed…
 Why?
Understanding Reference Semantics

 There’s a lot going on with x = 3


 An integer 3 is created and stored in memory
 A name x is created
 An reference to the memory location storing
the 3 is then assigned to the name x
 So: When we say that the value of x is 3
 we mean that x now refers to the integer 3
Name: x Type: Integer
Ref: <address1> Data: 3

name list memory


Understanding Reference Semantics
 The data 3 we created is of type integer –
objects are typed, variables are not
 In Python, the datatypes integer, float, and
string (and tuple) are “immutable”
 This doesn’t mean we can’t change the value
of x, i.e. change what x refers to …
 For example, we could increment x:
>>> x = 3
>>> x = x + 1
>>> print x
4
Understanding Reference Semantics
When we increment x, then what happens is:
1. The reference of name x is looked up.
2. The value at that reference is retrieved.

Type: Integer
Name: x Data: 3
Ref: <address1>

>>> x = x + 1
Understanding Reference Semantics
When we increment x, then what happening is:
1. The reference of name x is looked up.
2. The value at that reference is retrieved.
3. The 3+1 calculation occurs, producing a new
data element 4 which is assigned to a fresh
memory location with a new reference

Type: Integer
Name: x Data: 3
Ref: <address1>
Type: Integer
Data: 4

>>> x = x + 1
Understanding Reference Semantics
When we increment x, then what happening is:
1. The reference of name x is looked up.
2. The value at that reference is retrieved.
3. The 3+1 calculation occurs, producing a new
data element 4 which is assigned to a fresh
memory location with a new reference
4. The name x is changed to point to new ref

Type: Integer
Name: x Data: 3
Ref: <address1>
Type: Integer
Data: 4

>>> x = x + 1
Assignment
So, for simple built-in datatypes (integers, floats,
strings) assignment behaves as expected
>>> x = 3 # Creates 3, name x refers to 3

>>> y = x # Creates name y, refers to 3


>>> y = 4 # Creates ref for 4. Changes y
>>> print x # No effect on x, still ref 3
3
Assignment
So, for simple built-in datatypes (integers, floats,
strings) assignment behaves as expected
>>> x = 3 # Creates 3, name x refers to 3

>>> y = x # Creates name y, refers to 3


>>> y = 4 # Creates ref for 4. Changes y
>>> print x # No effect on x, still ref 3
3
Name: x
Ref: <address1> Type: Integer
Data: 3
Assignment
So, for simple built-in datatypes (integers, floats,
strings) assignment behaves as expected
>>> x = 3 # Creates 3, name x refers to 3

>>> y = x # Creates name y, refers to 3


>>> y = 4 # Creates ref for 4. Changes y
>>> print x # No effect on x, still ref 3
3
Name: x
Ref: <address1> Type: Integer
Data: 3
Name: y
Ref: <address2>
Assignment
So, for simple built-in datatypes (integers, floats,
strings) assignment behaves as expected
>>> x = 3 # Creates 3, name x refers to 3

>>> y = x # Creates name y, refers to 3


>>> y = 4 # Creates ref for 4. Changes y
>>> print x # No effect on x, still ref 3
3
Name: x
Ref: <address1> Type: Integer
Data: 3
Name: y
Type: Integer
Ref: <address2>
Data: 4
Assignment
So, for simple built-in datatypes (integers, floats,
strings) assignment behaves as expected
>>> x = 3 # Creates 3, name x refers to 3

>>> y = x # Creates name y, refers to 3


>>> y = 4 # Creates ref for 4. Changes y
>>> print x # No effect on x, still ref 3
3
Name: x
Ref: <address1> Type: Integer
Data: 3
Name: y
Type: Integer
Ref: <address2>
Data: 4
Assignment
So, for simple built-in datatypes (integers, floats,
strings) assignment behaves as expected
>>> x = 3 # Creates 3, name x refers to 3

>>> y = x # Creates name y, refers to 3


>>> y = 4 # Creates ref for 4. Changes y
>>> print x # No effect on x, still ref 3
3
Name: x
Ref: <address1> Type: Integer
Data: 3
Name: y
Type: Integer
Ref: <address2>
Data: 4
Assignment
So, for simple built-in datatypes (integers, floats,
strings) assignment behaves as expected
>>> x = 3 # Creates 3, name x refers to 3

>>> y = x # Creates name y, refers to 3


>>> y = 4 # Creates ref for 4. Changes y
>>> print x # No effect on x, still ref 3
3
Name: x
Ref: <address1> Type: Integer
Data: 3
Name: y
Type: Integer
Ref: <address2>
Data: 4
Assignment & mutable objects
For other data types (lists, dictionaries, user-defined
types), assignment works differently
• These datatypes are “mutable”
• Change occur in place
• We don’t copy them into a new memory address each time
• If we type y=x and then modify y, both x and y are changed

>>> x = 3 x = some mutable object


>>> y = x y = x
immutable
>>> y = 4 mutable
make a change to y
>>> print x look at x
3 x will be changed as well
Why? Changing a Shared List

a = [1, 2, 3] a 1 2 3

a
b=a 1 2 3
b

a
a.append(4) 1 2 3 4
b
Surprising example surprising no more

So now, here’s our code:

>>> a = [1, 2, 3] # a now references the list [1,


2, 3]
>>> b = a # b now references what a
references
>>> a.append(4) # this changes the list a
references
>>> print b # if we print what b
references,
[1, 2, 3, 4] # SURPRISE! It has
changed…

You might also like