Pass by Object Reference in Python - University of Tulsa
Pass by Object Reference in Python
Tyler Moore
CS 2123, The University of Tulsa
Variables in Python
Better thought of as names or identifiers attached to an object. A nice explanation: idiomatic/handout.html#other-languages-have-variables
2/8
Key distinction: mutable vs. immutable objects
Immutable: objects whose value cannot change 1 Tuples (makes sense) 2 Booleans (surprise?) 3 Numbers (surprise?) 4 Strings (surprise?)
Mutable: objects whose value can change 1 Dictionaries 2 Lists 3 User-defined objects (unless defined as immutable)
This distinction matters because it explains seemingly contradictory behavior
3/8
Variable assignment in action
>>> #variables are really names
... c = 4
>>> d = c
>>> c+=1
>>> c
5
>>> d
#d does not change because numbers are immutable
4
>>> #lists are mutable
... a = [1,4,2]
>>> b = a
#so this assigns the name b to the object attached to name a
>>> a.append(3)
>>> a
[1, 4, 2, 3]
>>> b #b still points to the same object, its contents have just changed.
[1, 4, 2, 3]
4/8
Pass by object reference
In Python, variables are not passed by reference or by value Instead, the name (aka object reference) is passed If the underlying object is mutable, then modifications to the object will persist If the underlying object is immutable, then changes to the variable do not persist For more info, see: is-python-callbyvalue-or-callbyreference-neither/
5/8
Im/mutablility and function calls
>>> #let's try this in a function
... def increment(n): #n is a name assigned to the function argument when called
...
#because numbers are immutable, the following
...
#reassigns n to the number represented by n+1
...
n+=1
...
return n
...
>>> a = 3
>>> increment(a)
4
>>> #a does not change
... a
3
6/8
Im/mutablility and function calls
>>> def sortfun(s):
...
s.sort()
...
return s
...
>>> def sortfun2(s):
...
l = list(s)
...
l.sort()
...
return l
...
>>> a = [1,4,2]
>>> sortfun(a)
[1, 2, 4]
>>> a
[1, 2, 4]
>>> b = [3,9,1]
>>> sortfun2(b)
[1, 3, 9]
>>> b
[3, 9, 1]
7/8
Im/mutablility and function calls
def selection_sort(s): """ Input: list s to be sorted Output: sorted list """ for i in range(len(s)): #don't name min since reserved word minidx=i for j in range(i+1,len(s)): if s[j]>> b [3, 9, 1] >>> selection_sort(b) [1, 3, 9] >>> b [1, 3, 9]
8/8
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- python object oriented picone press
- modeling physical objects with object oriented programming
- ppyytthhoonn oobbjjeecctt oorriieenntteedd
- pass by object reference in python university of tulsa
- object oriented python an introduction purdue university college of
- python mongodb tutorial
- object oriented programming in python babraham institute
- mit6 0001f16 object oriented programming mit opencourseware
- pyopt a python based object oriented framework for nonlinear
- object oriented design
Related searches
- is python pass by reference
- python pass by object reference
- university of tulsa continuing education
- python pass by value
- object reference not set instance of object
- how to fix object reference not set
- null object reference fnf
- unity object reference not set to instance
- null object reference error
- what does object reference not set mean
- object reference not set to an instance
- object reference not set to