Reference semantics, variables and names - PowerPoint PPT Presentation

About This Presentation
Title:

Reference semantics, variables and names

Description:

Reference semantics, variables and names Some material adapted from Upenn cmpe391 s and other sources Understanding Reference Semantics Assignment manipulates ... – PowerPoint PPT presentation

Number of Views:215
Avg rating:3.0/5.0
Slides: 18
Provided by: MattHuen4
Category:

less

Transcript and Presenter's Notes

Title: Reference semantics, variables and names


1
Reference semantics, variables and names
Some material adapted from Upenn cmpe391 slides
and other sources
2
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.
  • gtgtgt a 1, 2, 3 a now references the list 1,
    2, 3
  • gtgtgt b a b now references what a references
  • gtgtgt a.append(4) this changes the list a
    references
  • gtgtgt print b if we print what b references,
  • 1, 2, 3, 4 SURPRISE! It has changed
  • Why?

3
Understanding Reference Semantics
  • Theres 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

4
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 doesnt mean we cant change the value of x,
    i.e. change what x refers to
  • For example, we could increment x
  • gtgtgt x 3
  • gtgtgt x x 1
  • gtgtgt print x
  • 4

5
Understanding Reference Semantics
  • When we increment x, then what happens is
  • The reference of name x is looked up.
  • The value at that reference is retrieved.

Type Integer Data 3
Name x Ref ltaddress1gt
gtgtgt x x 1
6
Understanding Reference Semantics
  • When we increment x, then what happening is
  • The reference of name x is looked up.
  • The value at that reference is retrieved.
  • The 31 calculation occurs, producing a new data
    element 4 which is assigned to a fresh memory
    location with a new reference

Type Integer Data 3
Name x Ref ltaddress1gt
Type Integer Data 4
gtgtgt x x 1
7
Understanding Reference Semantics
  • When we increment x, then what happening is
  • The reference of name x is looked up.
  • The value at that reference is retrieved.
  • The 31 calculation occurs, producing a new data
    element 4 which is assigned to a fresh memory
    location with a new reference
  • The name x is changed to point to new ref

Type Integer Data 3
Name x Ref ltaddress1gt
Type Integer Data 4
gtgtgt x x 1
8
Assignment
  • So, for simple built-in datatypes (integers,
    floats, strings) assignment behaves as expected
  • gtgtgt x 3 Creates 3, name x refers to 3 gtgtgt y
    x Creates name y, refers to 3gtgtgt y 4
    Creates ref for 4. Changes ygtgtgt print x No
    effect on x, still ref 33

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

Name x Ref ltaddress1gt
Type Integer Data 3
10
Assignment
  • So, for simple built-in datatypes (integers,
    floats, strings) assignment behaves as expected
  • gtgtgt x 3 Creates 3, name x refers to 3 gtgtgt y
    x Creates name y, refers to 3gtgtgt y 4
    Creates ref for 4. Changes ygtgtgt print x No
    effect on x, still ref 33

Name x Ref ltaddress1gt
Type Integer Data 3
Name y Ref ltaddress2gt
11
Assignment
  • So, for simple built-in datatypes (integers,
    floats, strings) assignment behaves as expected
  • gtgtgt x 3 Creates 3, name x refers to 3 gtgtgt y
    x Creates name y, refers to 3gtgtgt y 4
    Creates ref for 4. Changes ygtgtgt print x No
    effect on x, still ref 33

Name x Ref ltaddress1gt
Type Integer Data 3
Name y Ref ltaddress2gt
Type Integer Data 4
12
Assignment
  • So, for simple built-in datatypes (integers,
    floats, strings) assignment behaves as expected
  • gtgtgt x 3 Creates 3, name x refers to 3 gtgtgt y
    x Creates name y, refers to 3gtgtgt y 4
    Creates ref for 4. Changes ygtgtgt print x No
    effect on x, still ref 33

Name x Ref ltaddress1gt
Type Integer Data 3
Name y Ref ltaddress2gt
Type Integer Data 4
13
Assignment
  • So, for simple built-in datatypes (integers,
    floats, strings) assignment behaves as expected
  • gtgtgt x 3 Creates 3, name x refers to 3 gtgtgt y
    x Creates name y, refers to 3gtgtgt y 4
    Creates ref for 4. Changes ygtgtgt print x No
    effect on x, still ref 33

Name x Ref ltaddress1gt
Type Integer Data 3
Name y Ref ltaddress2gt
Type Integer Data 4
14
Assignment
  • So, for simple built-in datatypes (integers,
    floats, strings) assignment behaves as expected
  • gtgtgt x 3 Creates 3, name x refers to 3 gtgtgt y
    x Creates name y, refers to 3gtgtgt y 4
    Creates ref for 4. Changes ygtgtgt print x No
    effect on x, still ref 33

Name x Ref ltaddress1gt
Type Integer Data 3
Name y Ref ltaddress2gt
Type Integer Data 4
15
Assignment mutable objects
  • For other data types (lists, dictionaries,
    user-defined types), assignment works differently
  • These datatypes are mutable
  • Change occur in place
  • We dont copy them into a new memory address each
    time
  • If we type yx and then modify y, both x and y
    are changed
  • gtgtgt x 3 x some mutable object
  • gtgtgt y x y x
  • gtgtgt y 4 make a change to y
  • gtgtgt print x look at x
  • 3 x will be changed as well

immutable
mutable
16
Why? Changing a Shared List
a 1, 2, 3
b a
a
1
2
3
4
a.append(4)
b
17
Surprising example surprising no more
  • So now, heres our code
  • gtgtgt a 1, 2, 3 a now references the list 1,
    2, 3
  • gtgtgt b a b now references what a
    references
  • gtgtgt a.append(4) this changes the list a
    references
  • gtgtgt print b if we print what b
    references,
  • 1, 2, 3, 4 SURPRISE! It has
    changed
Write a Comment
User Comments (0)
About PowerShow.com