Git Product home page Git Product logo

Comments (4)

tylerjarvis avatar tylerjarvis commented on June 20, 2024

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.

shanemcq18 avatar shanemcq18 commented on June 20, 2024

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.

shanemcq18 avatar shanemcq18 commented on June 20, 2024

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.

groutr avatar groutr commented on June 20, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.