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
2
3
4
Tuples (makes sense)
Booleans (surprise?)
Numbers (surprise?)
Strings (surprise?)
Mutable: objects whose value can change
1
2
3
Dictionaries
Lists
User-defined objects (unless defined as immutable)
This distinction matters because it explains seemingly contradictory
behavior
3/8
Variable assignment in action
>>>
...
>>>
>>>
>>>
5
>>>
4
>>>
...
>>>
>>>
>>>
[1,
>>>
[1,
#variables are really names
c = 4
d = c
c+=1
c
d
#d does not change because numbers are immutable
#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
4, 2, 3]
b #b still points to the same object, its contents have just changed.
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
................
................
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
- object internals in python europython 2021
- python object oriented programming 2 iit delhi
- sorting and classifying by attributes a co teaching lesson plan
- objectoriented programming in python iit delhi
- python classes and objects george mason university
- object oriented design with python computer science
- use python with r with reticulate cheat sheet github
- oop in python tutorials point
- pass by object reference in python university of tulsa
- object oriented programming in python defining classes
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