Comments (4)
Ryan is right. This needs to be fixed—not sure how this got in there. We’d better check for other such errors
from labs.
A few articles for us to read as we fix this:
https://medium.com/@meghamohan/mutable-and-immutable-side-of-python-c2145cf72747
https://codehabitude.com/2013/12/24/python-objects-mutable-vs-immutable/
http://net-informations.com/python/iq/immutable.htm
from labs.
Here's an excerpt from a newer version of the lab (being merged in soon):
Every Python object type falls into one of two categories: a mutable object may be altered at any time, while an immutable object cannot be altered once created. Attempting to change an immutable object creates a new object in memory. If two names refer to the same mutable object, any changes to the object are reflected in both names since they still both refer to that same object. On the other hand, if two names refer to the same immutable object and one of the values is "changed," one name will refer to the original object, and the other will refer to a new object in memory.
The more I look at this lab, the more I want to rewrite some of it to do a better treatment of namespaces and mutability anyway, but this should be a sufficient patch for now.
from labs.
@shanemcq18, there is an important subtlety that needs to be addressed. What happens when your immutable object contains a reference to a mutable object? The description needs to emphasize that what makes a container immutable is the inability to change the references after object creation. However, if you are pointing to a mutable object inside an immutable container, then you can still change the mutable object since the reference stays the same.
a = [1]
b = (1, a)
# b is (1, [1])
a.append(2)
# b is now (1, [1, 2])
c = b + (4,) # c is a new tuple: (1, [1, 2], 4)
a.append(3)
# b is now (1, [1, 2, 3])
# c is now (1, [1, 2, 3], 4)
It would be very easy for a beginner to assume that b
does not change after updating a
, because b
is immutable. Even though b
is an immutable object, it can't be used as a key in a dictionary or member of a set.
from labs.
Related Issues (15)
- Suggestion: Update Exceptions_FileIO lab
- ObjectOriented - solutions file
- Exceptions_fileIO - spec file (maybe pdf)
- PythonAndNumpy2 - pdf
- Linear Transformation - pdf
- Exceptions and File IO - pdf
- Python Essentials, named arguments HOT 1
- Exceptions and File IO
- QR Decomposition - pdf
- Breadth First Search - pdf
- Breadth First Search - test driver
- Breadth First Search - PDF
- Typo in profiling exercise HOT 2
- Suggestion: Move labs to Jupyter Book format HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from labs.