Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Viraj Deshaval
Viraj Deshaval
4,874 Points

Correction required in the video

Craig Dennis I believe the 'del' example showed in the video is little miguided.

What del does here is as below:

books = [
    "Learning Python: Powerful Object-Oriented Programming - Mark Lutz",
    "Automate the Boring Stuff with Python: Practical Programming for Total Beginners - Al Sweigart",
    "Python for Data Analysis - Wes McKinney",
    "Fluent Python: Clear, Concise, and Effective Programming - Luciano Ramalho",
    "Python for Kids: A Playful Introduction To Programming - Jason R. Briggs",
    "Hello Web App: Learn How to Build a Web App - Tracy Osborn",
]                                         

>>> book1 = books[0]                                                                                                           
>>> book1                                                                                                                      
'Learning Python: Powerful Object-Oriented Programming - Mark Lutz'

This is not like string literal 'Learning Python: Powerful Object-Oriented Programming - Mark Lutz' have two labels i.e. books[0] and book1 this is like there are two string literal in memory pointing to two different labels i.e.

books[0] = 'Learning Python: Powerful Object-Oriented Programming - Mark Lutz'
and book1 = 'Learning Python: Powerful Object-Oriented Programming - Mark Lutz'

We can think of like two same cube of cheese in a cheese shop i.e. cheese 1 with label 1 and cheese 2 with label 2.

so now let's say when we delete the 'del books[0]' it will remove the first item from the list but not the other one because those are two separate things. I believe this needs to be corrected in the video. Please correct me if I am wrong.

1 Answer

Steven Parker
Steven Parker
231,275 Points

In this case, both variables reference the same string. You can demonstrate this using the identity operator, "is":

>>> book1 = books[0]
>>> book1 is books[0]  # same object?
True
>>> book2 = "Learning Python: Powerful Object-Oriented Programming - Mark Lutz"
>>> book2 == books[0]  # same value?
True
>>> book2 is books[0]  # same object?
False

Note that your example creates a new variable that the identity operator confirms is a reference to the same string literal. But if we create another variable using a new string literal (that happens to have the same exact contents), the identity operator shows that they are not the same.

So "book[0]" and "book1" are two labels for the same cube of cheese, but "book2" is a different cheese. :wink:

Viraj Deshaval
Viraj Deshaval
4,874 Points

Hi Steve, Yes that's correct so if you see the video you will come to know what I am talking about here and why. I believe this needs some correction.

Steven Parker
Steven Parker
231,275 Points

You didn't provide a link to the video, so perhaps I misunderstood which point was yours.

Viraj Deshaval
Viraj Deshaval
4,874 Points

Sorry Steve my bad. Link to the Video that teaches you about deletion is as below: https://teamtreehouse.com/library/deletion

Steven Parker
Steven Parker
231,275 Points

The video seems to be consistent with what we have been discussing here. In the video, Craig says, "So I'm going to say carne_asada = craigs_lunch and we'll see that carne_asada now also points to that same taco."

Then he goes on to demonstrate how deleting the label doesn't affect the underlying object as long as other labels still exist.

This all seems very much like the examples of "book[0]" and "book1" here. I don't see any errors in the video.