title
Mutability revisited - Python 3 Programming Tutorial p.8

description
In this part, we're going to revisit the topic of mutable and immutable objects. This concept is masked pretty well in Python, which, like dynamic typing, can be great... or not. It can really bite you one day if you don't have a good understanding of how it works, so let's talk about it. Playlist: https://www.youtube.com/playlist?list=PLQVvvaa0QuDeAams7fkdcwOGBpGdHpXln #python #programming #tutorial quiz: x = 1 def test(): x = 2 test() print(x) x = 1 def test(): global x x = 2 test() print(x) x = [1] def test(): x = [2] test() print(x) x = [1] def test(): global x x = [2] test() print(x) x = [1] def test(): x[0] = 2 test() print(x)

detail
{'title': 'Mutability revisited - Python 3 Programming Tutorial p.8', 'heatmap': [{'end': 256.1, 'start': 230.125, 'weight': 0.756}, {'end': 749.848, 'start': 716.072, 'weight': 0.762}, {'end': 883.27, 'start': 784.495, 'weight': 0.856}, {'end': 962.018, 'start': 917.398, 'weight': 0.719}], 'summary': "In the video tutorial 'mutability revisited - python 3 programming tutorial p.8', the importance of understanding mutability in python 3 is emphasized to avoid frustration and coding errors, illustrated by challenges of modifying variables outside of functions, troubleshooting game code, and efficient programming practices for writing error-free code and improving code quality.", 'chapters': [{'end': 80.87, 'segs': [{'end': 60.493, 'src': 'embed', 'start': 2.125, 'weight': 0, 'content': [{'end': 8.587, 'text': "What's going on everybody and welcome to part 8 of our Python 3 basics tutorial series.", 'start': 2.125, 'duration': 6.462}, {'end': 9.647, 'text': 'In this video.', 'start': 8.927, 'duration': 0.72}, {'end': 15.948, 'text': "what we're going to be talking about is kind of revisiting the concept of mutability,", 'start': 9.647, 'duration': 6.301}, {'end': 28.732, 'text': "because I think it's pretty important that we don't get through the basics series using the methodology that we're using and not address Mutability.", 'start': 15.948, 'duration': 12.784}, {'end': 38.237, 'text': "I'm not sure if that logically made sense that string there, But basically, the way that we're doing things right now is is we're modifying this game,", 'start': 28.732, 'duration': 9.505}, {'end': 43.44, 'text': 'This game variable, outside of this function.', 'start': 38.237, 'duration': 5.203}, {'end': 54.586, 'text': "here We're able to do that because it's mutable, but this Can be very tricky at times and later on,", 'start': 43.44, 'duration': 11.146}, {'end': 60.493, 'text': "If you continue to code with the assumption that that's going to work out for you eventually,", 'start': 55.171, 'duration': 5.322}], 'summary': 'Part 8 of python 3 basics tutorial revisits the concept of mutability in programming.', 'duration': 58.368, 'max_score': 2.125, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/xwPWcFKeIac/pics/xwPWcFKeIac2125.jpg'}], 'start': 2.125, 'title': 'Revisiting mutability in python 3', 'summary': 'Emphasizes the importance of understanding mutability in python 3 to avoid frustration and coding errors, illustrated by challenges of modifying variables outside of functions.', 'chapters': [{'end': 80.87, 'start': 2.125, 'title': 'Python 3 basics: revisiting mutability', 'summary': 'Revisits the concept of mutability in python, emphasizing the importance of understanding it to avoid frustration and coding errors, as illustrated by the challenges of modifying variables outside of functions.', 'duration': 78.745, 'highlights': ['The importance of understanding mutability is emphasized to avoid frustration and coding errors.', 'Modifying variables outside of functions can lead to challenges and frustrations, causing confusion and coding errors.']}], 'duration': 78.745, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/xwPWcFKeIac/pics/xwPWcFKeIac2125.jpg', 'highlights': ['The importance of understanding mutability is emphasized to avoid frustration and coding errors.', 'Modifying variables outside of functions can lead to challenges and frustrations, causing confusion and coding errors.']}, {'end': 406.445, 'segs': [{'end': 108.188, 'src': 'embed', 'start': 82.491, 'weight': 0, 'content': [{'end': 87.972, 'text': "So I think I would be doing a pretty big disservice if I didn't at least bring it up because it's probably not gonna come up again.", 'start': 82.491, 'duration': 5.481}, {'end': 91.433, 'text': "And arguably, I don't think this has ever come up in any tutorial.", 'start': 88.952, 'duration': 2.481}, {'end': 94.754, 'text': "I've never really addressed this, but it can bite you.", 'start': 91.453, 'duration': 3.301}, {'end': 96.435, 'text': "So let's talk about it.", 'start': 95.334, 'duration': 1.101}, {'end': 108.188, 'text': "So sometimes you might see so like if you want to modify a value outside of a function, if it's a list of lists,", 'start': 96.916, 'duration': 11.272}], 'summary': 'The need to modify values outside a function is often overlooked in tutorials.', 'duration': 25.697, 'max_score': 82.491, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/xwPWcFKeIac/pics/xwPWcFKeIac82491.jpg'}, {'end': 173.198, 'src': 'embed', 'start': 138.595, 'weight': 2, 'content': [{'end': 144.377, 'text': "and then i'm gonna show you what i think would be a superior method of kind of building these kinds of things.", 'start': 138.595, 'duration': 5.782}, {'end': 156.88, 'text': "so, First of all, I'll just comment those out in multi-line comment, make some space, and in fact, what I'll do is I'm going to copy all of this.", 'start': 144.377, 'duration': 12.503}, {'end': 164.202, 'text': "I'm just going to copy that up here, paste, and then we're going to run GameBoard.", 'start': 157.28, 'duration': 6.922}, {'end': 173.198, 'text': 'Just display true for now.', 'start': 170.195, 'duration': 3.003}], 'summary': 'Demonstrating a superior method for building with a focus on gameboard display.', 'duration': 34.603, 'max_score': 138.595, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/xwPWcFKeIac/pics/xwPWcFKeIac138595.jpg'}, {'end': 256.1, 'src': 'heatmap', 'start': 204.767, 'weight': 3, 'content': [{'end': 206.868, 'text': 'So I want to show an exact change.', 'start': 204.767, 'duration': 2.101}, {'end': 209.189, 'text': "That's OK.", 'start': 208.589, 'duration': 0.6}, {'end': 211.249, 'text': 'So anyways, game board.', 'start': 209.509, 'duration': 1.74}, {'end': 213.93, 'text': 'We print game board, and we can see no change has been made.', 'start': 211.349, 'duration': 2.581}, {'end': 228.905, 'text': 'And then if we were to run player 1, row 1, column 1, and what if we can print um game here and then after running game board,', 'start': 213.97, 'duration': 14.935}, {'end': 230.125, 'text': 'we can print game again?', 'start': 228.905, 'duration': 1.22}, {'end': 235.688, 'text': "so we'll have like three games, basically, and we can see initially, okay.", 'start': 230.125, 'duration': 5.563}, {'end': 237.929, 'text': "and then here's the pretty modified version.", 'start': 235.688, 'duration': 2.241}, {'end': 244.553, 'text': 'and then here is, after the function has run yet again, a version where that one is indeed modified in there.', 'start': 237.929, 'duration': 6.624}, {'end': 247.454, 'text': "Now, what I'd like to do is change this.", 'start': 245.413, 'duration': 2.041}, {'end': 250.756, 'text': "So we're going to change first the variable here of game.", 'start': 247.514, 'duration': 3.242}, {'end': 256.1, 'text': "And we're going to make it instead of string, I want to play a game.", 'start': 251.757, 'duration': 4.343}], 'summary': 'Modifying game board to play a game instead of string.', 'duration': 30.921, 'max_score': 204.767, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/xwPWcFKeIac/pics/xwPWcFKeIac204767.jpg'}, {'end': 380.06, 'src': 'embed', 'start': 305.28, 'weight': 1, 'content': [{'end': 306.142, 'text': "Let's go ahead and run it though.", 'start': 305.28, 'duration': 0.862}, {'end': 311.557, 'text': "And we can see here, no, that's not true.", 'start': 308.134, 'duration': 3.423}, {'end': 322.406, 'text': "What if we print game here? Because a moment ago we were somehow able to access the game, but now we're unable to access game early on.", 'start': 312.318, 'duration': 10.088}, {'end': 324.388, 'text': 'So immediately we know something is afoot.', 'start': 322.466, 'duration': 1.922}, {'end': 328.612, 'text': 'And let me run this again.', 'start': 325.589, 'duration': 3.023}, {'end': 334.276, 'text': "But then what if we print game after we've set a game?", 'start': 330.553, 'duration': 3.723}, {'end': 339.774, 'text': "Okay, so we're thinking everything's good at this point, and then we get down here and we're back to.", 'start': 335.45, 'duration': 4.324}, {'end': 340.915, 'text': 'I want to play a game.', 'start': 339.774, 'duration': 1.141}, {'end': 345.058, 'text': "That's because this string is immutable.", 'start': 341.535, 'duration': 3.523}, {'end': 346.5, 'text': "We can't go and change that.", 'start': 345.559, 'duration': 0.941}, {'end': 355.42, 'text': 'so, um so this can start to kind of run you into some trouble, uh, later on.', 'start': 349.255, 'duration': 6.165}, {'end': 357.341, 'text': 'so like, what if we were to do this?', 'start': 355.42, 'duration': 1.921}, {'end': 358.943, 'text': 'so what if we said print?', 'start': 357.341, 'duration': 1.602}, {'end': 366.308, 'text': 'and one thing we can do is we can always check the unique id of a element in python using the built-in function id.', 'start': 358.943, 'duration': 7.365}, {'end': 374.335, 'text': "so we'll check the id of game here and then we can check the id of game here And then maybe at the very end again.", 'start': 366.308, 'duration': 8.027}, {'end': 378.779, 'text': 'So start thinking about okay, what do you think?', 'start': 375.136, 'duration': 3.643}, {'end': 380.06, 'text': 'which IDs are going to be the same??', 'start': 378.779, 'duration': 1.281}], 'summary': 'Troubleshooting code reveals immutability issue, leading to potential trouble and need for unique id checks.', 'duration': 74.78, 'max_score': 305.28, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/xwPWcFKeIac/pics/xwPWcFKeIac305280.jpg'}], 'start': 82.491, 'title': 'Modifying values outside functions and troubleshooting game code', 'summary': 'Discusses modifying values outside of functions, demonstrating a superior method with examples. it also covers troubleshooting game code, showcasing the usage of the unique id function in python and potential issues with variable access and assignment.', 'chapters': [{'end': 261.223, 'start': 82.491, 'title': 'Modifying values outside functions', 'summary': 'Discusses the issue of modifying values outside of functions, highlighting the potential problems and demonstrating a superior method, with examples and the impact of changes.', 'duration': 178.732, 'highlights': ['The tutorial addresses the issue of modifying values outside of functions, emphasizing that it can lead to potential problems and has never been discussed before.', 'The transcript provides examples of modifying a list of lists and demonstrates the impact of changes on the game board.', 'It introduces a superior method for building these kinds of things, showcasing the change in variable type and its impact on the game board.']}, {'end': 406.445, 'start': 261.283, 'title': 'Troubleshooting game code', 'summary': 'Discusses troubleshooting game code, showcasing the usage of the unique id function in python to identify immutable elements and potential issues with variable access and assignment.', 'duration': 145.162, 'highlights': ['The chapter demonstrates the troubleshooting of game code, revealing the usage of the unique id function in Python to identify immutable elements and potential issues with variable access and assignment.', 'The speaker mentions the inability to access the game early on, leading to the realization of an issue with variable assignment.', 'The transcript highlights the concept of immutability in Python strings, showcasing how it can lead to potential issues and troubleshooting scenarios.', 'The usage of the built-in function id in Python is showcased, with the speaker checking the unique id of the game variable at different points in the code for comparison and troubleshooting purposes.']}], 'duration': 323.954, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/xwPWcFKeIac/pics/xwPWcFKeIac82491.jpg', 'highlights': ['The tutorial addresses the issue of modifying values outside of functions, emphasizing potential problems.', 'The chapter demonstrates troubleshooting game code, revealing the usage of the unique id function in Python.', 'It introduces a superior method for building things, showcasing the change in variable type and its impact on the game board.', 'The transcript provides examples of modifying a list of lists and demonstrates the impact of changes on the game board.', 'The speaker mentions the inability to access the game early on, leading to the realization of an issue with variable assignment.', 'The transcript highlights the concept of immutability in Python strings, showcasing potential issues and troubleshooting scenarios.']}, {'end': 1055.392, 'segs': [{'end': 749.848, 'src': 'heatmap', 'start': 716.072, 'weight': 0.762, 'content': [{'end': 728.579, 'text': 'I also have passed, in the text-based version of this tutorial, I put in a little quiz that Daniel or Danos from the Discord wrote up.', 'start': 716.072, 'duration': 12.507}, {'end': 730.58, 'text': "And I think it's actually a pretty good quiz.", 'start': 728.959, 'duration': 1.621}, {'end': 736.423, 'text': "So if you want, I'll have the link to the text-based version of this tutorial in the description.", 'start': 731.521, 'duration': 4.902}, {'end': 740.886, 'text': 'Go there and copy and paste this code into some script or something.', 'start': 736.844, 'duration': 4.042}, {'end': 749.848, 'text': "And beside all the X's or on a sheet of paper or whatever, say, OK, here's what I think X is going to be equal to.", 'start': 742.285, 'duration': 7.563}], 'summary': 'The tutorial includes a quiz with a link to the text-based version for copying and pasting code.', 'duration': 33.776, 'max_score': 716.072, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/xwPWcFKeIac/pics/xwPWcFKeIac716072.jpg'}, {'end': 883.27, 'src': 'heatmap', 'start': 777.83, 'weight': 1, 'content': [{'end': 784.495, 'text': 'But my argument is, if you missed anything on this little quiz or you had to think kind of hard about it,', 'start': 777.83, 'duration': 6.665}, {'end': 793.642, 'text': "chances are when you're like zooming through writing a program – that's how I program, by the way – um you are highly likely to make the mistake.", 'start': 784.495, 'duration': 9.147}, {'end': 803.968, 'text': 'so the whole point of this tutorial has been one to draw your attention to how you can run yourself amok um with mutability.', 'start': 793.642, 'duration': 10.326}, {'end': 816.044, 'text': "but also, I would argue, we should go ahead and just pass game here, or maybe better put, let's call it game map or apparently, game board.", 'start': 803.968, 'duration': 12.076}, {'end': 817.925, 'text': "Where did that come from? Oh, that's the name of the function.", 'start': 816.064, 'duration': 1.861}, {'end': 819.626, 'text': "Let's not do that.", 'start': 818.906, 'duration': 0.72}, {'end': 820.667, 'text': "So we'll call it game map.", 'start': 819.827, 'duration': 0.84}, {'end': 822.889, 'text': 'So we pass game map.', 'start': 821.207, 'duration': 1.682}, {'end': 827.312, 'text': "I would argue let's go ahead and call this game map.", 'start': 823.369, 'duration': 3.943}, {'end': 831.755, 'text': "So we're going to modify this temporary game map variable here.", 'start': 827.712, 'duration': 4.043}, {'end': 846.519, 'text': "Game map game map enumerate game map and then when we're all done return Game map now again in this exact problem in this exact code.", 'start': 832.955, 'duration': 13.564}, {'end': 857.707, 'text': 'This is not really doing anything special for you, but Unless you can write perfect code or take that quiz and get it right every time and always,', 'start': 847.079, 'duration': 10.628}, {'end': 858.788, 'text': 'never make that mistake.', 'start': 857.707, 'duration': 1.081}, {'end': 862.992, 'text': "I'm just going to argue that maybe you want to get in the habit of doing things this way.", 'start': 858.788, 'duration': 4.204}, {'end': 869.137, 'text': 'Or do you want to check it every time? But in general, this is the way I would just write everything.', 'start': 863.172, 'duration': 5.965}, {'end': 873.361, 'text': 'It may not be necessary to do it in all cases, but I would do that.', 'start': 869.417, 'duration': 3.944}, {'end': 875.322, 'text': 'And honestly, I went through a good..', 'start': 873.781, 'duration': 1.541}, {'end': 883.27, 'text': 'probably four or five years of Python before I realized there was a thing going on there.', 'start': 877.324, 'duration': 5.946}], 'summary': 'Tutorial emphasizes avoiding mistakes and promoting good programming habits.', 'duration': 26.138, 'max_score': 777.83, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/xwPWcFKeIac/pics/xwPWcFKeIac777830.jpg'}, {'end': 1001.644, 'src': 'heatmap', 'start': 917.398, 'weight': 0, 'content': [{'end': 919.599, 'text': 'this is zoomed in like way too much for me.', 'start': 917.398, 'duration': 2.201}, {'end': 922.361, 'text': 'uh, type error missing one.', 'start': 919.599, 'duration': 2.762}, {'end': 924.722, 'text': "oh, we're not, okay, we didn't pass the actual game map.", 'start': 922.361, 'duration': 2.361}, {'end': 929.085, 'text': 'okay, um, so game, and then we pass game.', 'start': 924.722, 'duration': 4.363}, {'end': 933.77, 'text': 'Okay, and now everything acts in the exact same way.', 'start': 931.147, 'duration': 2.623}, {'end': 940.419, 'text': "So the idea here is you pass the game map or whatever object you're attempting to manipulate with your function.", 'start': 933.851, 'duration': 6.568}, {'end': 948.609, 'text': "you pass it, you modify that temporary parameter value or variable, And then you return it when you're done.", 'start': 940.419, 'duration': 8.19}, {'end': 956.054, 'text': "And then when you're using it, you say the thing that you're attempting to modify equals the result of that thing.", 'start': 949.189, 'duration': 6.865}, {'end': 957.675, 'text': 'And you pass that thing and all that.', 'start': 956.314, 'duration': 1.361}, {'end': 962.018, 'text': "So in terms of efficiency, I haven't run it.", 'start': 958.195, 'duration': 3.823}, {'end': 968.482, 'text': 'So if you were looking to scale a program to like, you know, millions of connections at a specific time.', 'start': 962.118, 'duration': 6.364}, {'end': 973.425, 'text': "This may not be the best way to do that, but a lot of code isn't like that.", 'start': 969.402, 'duration': 4.023}, {'end': 980.289, 'text': 'So, and this is really just a basics tutorial, but I would take that into consideration in terms of how you should be doing it.', 'start': 973.925, 'duration': 6.364}, {'end': 988.495, 'text': "Obviously, if the thing is immutable, we can't do it like with a string or something like that, then you will have to treat it this way.", 'start': 980.73, 'duration': 7.765}, {'end': 995.059, 'text': "But if you have something like this where we're just trying to modify a certain value in a list of lists, no, it's not required.", 'start': 989.055, 'duration': 6.004}, {'end': 1001.644, 'text': "But if you go through your programming tutorial thinking you can do everything this way, you're gonna quickly find out, no, no, no.", 'start': 995.859, 'duration': 5.785}], 'summary': 'Tutorial emphasizes passing and modifying game map, but may not be efficient for scaling to millions of connections.', 'duration': 67.793, 'max_score': 917.398, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/xwPWcFKeIac/pics/xwPWcFKeIac917398.jpg'}], 'start': 406.445, 'title': 'Efficient programming practices', 'summary': 'Emphasizes writing efficient and error-free code, demonstrating modifying temporary parameter values, which can help avoid mistakes and improve code quality, with insights into potential constraints for scaling programs.', 'chapters': [{'end': 454.629, 'start': 406.445, 'title': 'Understanding value modification in functions', 'summary': "Discusses the importance of modifying values within functions, as demonstrated by the example of a function not modifying the value of 'game' at the beginning, and the subsequent shift to working with lists instead.", 'duration': 48.184, 'highlights': ["The function in question did not modify the value of 'game' from the beginning, highlighting the significance of this issue.", 'The speaker demonstrates a shift to working with lists instead of printing IDs, emphasizing a practical approach to problem-solving.', "The chapter emphasizes the process of modifying 'game' to be a list with values 1, 2, and 3, showcasing the practical application of the discussed concepts."]}, {'end': 777.33, 'start': 454.629, 'title': 'Python list mutation and global variables', 'summary': 'Explores the concept of list mutability in python, demonstrating how modifying a list object differs from modifying its contents and the implications of using global variables, with examples of code execution and variable manipulation.', 'duration': 322.701, 'highlights': ['The chapter demonstrates the concept of list mutability in Python and how modifying a list object differs from modifying its contents. The chapter delves into the behavior of list mutability, showcasing examples of attempting to modify a list object and its contents in Python.', 'The implications of using global variables are discussed, with examples of code execution and variable manipulation. The chapter covers the implications of using global variables, showcasing how they can be globally accessible and manipulable within a function, with examples of code execution and variable manipulation.']}, {'end': 1055.392, 'start': 777.83, 'title': 'Efficient programming practices', 'summary': 'Emphasizes the importance of writing efficient and error-free code by demonstrating the process of modifying a temporary parameter value and returning it, suggesting that this method can help avoid mistakes and improve code quality, and provides insights into the potential constraints of the approach for scaling programs.', 'duration': 277.562, 'highlights': ['The chapter emphasizes the importance of writing efficient and error-free code by demonstrating the process of modifying a temporary parameter value and returning it. The speaker stresses the significance of writing code efficiently and correctly by showcasing the technique of modifying a temporary parameter value and returning it, which is intended to help programmers avoid mistakes and enhance the quality of their code.', 'The method is suggested to help avoid mistakes and improve code quality. The approach of modifying a temporary parameter value and returning it is proposed as a means to prevent errors and enhance the overall quality of the code, potentially leading to more robust and reliable programming practices.', 'Insights are provided into the potential constraints of the approach for scaling programs. The potential limitations of the method for scaling programs, particularly in the context of managing millions of connections, are discussed, highlighting the need to consider alternative approaches for optimizing efficiency in such scenarios.']}], 'duration': 648.947, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/xwPWcFKeIac/pics/xwPWcFKeIac406445.jpg', 'highlights': ['The chapter emphasizes the importance of writing efficient and error-free code by demonstrating the process of modifying a temporary parameter value and returning it.', 'The method is suggested to help avoid mistakes and improve code quality.', 'Insights are provided into the potential constraints of the approach for scaling programs.', 'The implications of using global variables are discussed, with examples of code execution and variable manipulation.', 'The chapter demonstrates the concept of list mutability in Python and how modifying a list object differs from modifying its contents.']}], 'highlights': ['The importance of understanding mutability is emphasized to avoid frustration and coding errors.', 'Modifying variables outside of functions can lead to challenges and frustrations, causing confusion and coding errors.', 'The tutorial addresses the issue of modifying values outside of functions, emphasizing potential problems.', 'The chapter demonstrates troubleshooting game code, revealing the usage of the unique id function in Python.', 'It introduces a superior method for building things, showcasing the change in variable type and its impact on the game board.', 'The transcript provides examples of modifying a list of lists and demonstrates the impact of changes on the game board.', 'The speaker mentions the inability to access the game early on, leading to the realization of an issue with variable assignment.', 'The transcript highlights the concept of immutability in Python strings, showcasing potential issues and troubleshooting scenarios.', 'The chapter emphasizes the importance of writing efficient and error-free code by demonstrating the process of modifying a temporary parameter value and returning it.', 'The method is suggested to help avoid mistakes and improve code quality.', 'Insights are provided into the potential constraints of the approach for scaling programs.', 'The implications of using global variables are discussed, with examples of code execution and variable manipulation.', 'The chapter demonstrates the concept of list mutability in Python and how modifying a list object differs from modifying its contents.']}