title
Python Tutorial: Duck Typing and Asking Forgiveness, Not Permission (EAFP)
description
In this Python Tutorial, we will look at a couple of the aspects of being "Pythonic". If you've never heard the term Pythonic before, basically it means that you are following conventions and coding styles of the Python language in order to write clean and readable code.
In this video we'll specifically be looking at two aspects of being Pythonic, and those are "Duck Typing" and the concept that it is easier to ask forgiveness than permission (EAFP). Let's get started.
The code from this video can be found at:
https://github.com/CoreyMSchafer/code_snippets/tree/master/EAFP
✅ Support My Channel Through Patreon:
https://www.patreon.com/coreyms
✅ Become a Channel Member:
https://www.youtube.com/channel/UCCezIgC97PvUuR4_gbFUs5g/join
✅ One-Time Contribution Through PayPal:
https://goo.gl/649HFY
✅ Cryptocurrency Donations:
Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot
✅ Corey's Public Amazon Wishlist
http://a.co/inIyro1
✅ Equipment I Use and Books I Recommend:
https://www.amazon.com/shop/coreyschafer
▶️ You Can Find Me On:
My Website - http://coreyms.com/
My Second Channel - https://www.youtube.com/c/coreymschafer
Facebook - https://www.facebook.com/CoreyMSchafer
Twitter - https://twitter.com/CoreyMSchafer
Instagram - https://www.instagram.com/coreymschafer/
#Python
detail
{'title': 'Python Tutorial: Duck Typing and Asking Forgiveness, Not Permission (EAFP)', 'heatmap': [{'end': 67.942, 'start': 41.062, 'weight': 0.797}, {'end': 288.215, 'start': 77.45, 'weight': 0.813}, {'end': 850.136, 'start': 828.726, 'weight': 0.805}], 'summary': "This tutorial covers pythonic coding principles, including duck typing, eafp, dictionary handling, and decision-making mindset, advocating for clean and readable code. it emphasizes error handling after attempting an action, pythonic dictionary handling, and the advantages of 'asking for forgiveness, not permission' mindset in python.", 'chapters': [{'end': 432.577, 'segs': [{'end': 67.942, 'src': 'heatmap', 'start': 21.762, 'weight': 0, 'content': [{'end': 27.991, 'text': 'And those are duck typing and the concept that it is easier to ask forgiveness than permission.', 'start': 21.762, 'duration': 6.229}, {'end': 32.375, 'text': "And sometimes you'll see people abbreviate this online as EAFP.", 'start': 28.511, 'duration': 3.864}, {'end': 38.041, 'text': "Now these two concepts are very closely related so that's why I'm putting them in the same video.", 'start': 33.878, 'duration': 4.163}, {'end': 40.682, 'text': "So first let's take a look at duck typing.", 'start': 38.481, 'duration': 2.201}, {'end': 49.667, 'text': "Now, the reason it's called duck typing is because with this concept you assume that if an object walks like a duck and quacks like a duck,", 'start': 41.062, 'duration': 8.605}, {'end': 50.328, 'text': "then it's a duck.", 'start': 49.667, 'duration': 0.661}, {'end': 57.834, 'text': "So what does that mean exactly? That means that we simply don't care what type of object we're working with.", 'start': 50.928, 'duration': 6.906}, {'end': 62.278, 'text': 'We only care if our object can do what we ask it to do.', 'start': 58.294, 'duration': 3.984}, {'end': 67.942, 'text': 'But I think this will be more clear if we take a look at an easy example here to see exactly what this means.', 'start': 62.818, 'duration': 5.124}], 'summary': 'Duck typing and eafp allow easy coding with minimal object type concern.', 'duration': 40.516, 'max_score': 21.762, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/x3v9zMX1s4s/pics/x3v9zMX1s4s21762.jpg'}, {'end': 288.215, 'src': 'heatmap', 'start': 77.45, 'weight': 0.813, 'content': [{'end': 86.159, 'text': 'and then I have a class Person, and it also has two methods, quack and fly, and both of these methods do different things depending on the class.', 'start': 77.45, 'duration': 8.709}, {'end': 90.643, 'text': 'Now, I also have a function here called quack and fly,', 'start': 86.72, 'duration': 3.923}, {'end': 99.793, 'text': "and the purpose of this function is that we're going to try to pass in this object thing and we're going to try to run the quack method and the fly method on it.", 'start': 90.643, 'duration': 9.15}, {'end': 107.399, 'text': "And you can see down here at the bottom, I'm making an instance of the class Duck, and I'm passing that into our function.", 'start': 100.253, 'duration': 7.146}, {'end': 113.283, 'text': "And then I'm also making an instance of the class Person, and I'm passing that into our function also.", 'start': 107.859, 'duration': 5.424}, {'end': 117.707, 'text': "So first let's take a look at something that is not Duck-typed.", 'start': 113.904, 'duration': 3.803}, {'end': 119.789, 'text': 'So this is non-Pythonic.', 'start': 118.067, 'duration': 1.722}, {'end': 129.495, 'text': "So in a non-Duck-typed example, we will specifically check if the object that we're working with an instance of a duck now this one.", 'start': 120.249, 'duration': 9.246}, {'end': 133.437, 'text': 'sure that we can use all of the methods and attributes that we want to use.', 'start': 129.495, 'duration': 3.942}, {'end': 141.4, 'text': "so you can see here right at the top, as soon as we enter the function, it's saying is this thing an instance of duck?", 'start': 133.437, 'duration': 7.963}, {'end': 147.443, 'text': "if so, then run these two methods and if it's not, then print that it has to be a duck.", 'start': 141.4, 'duration': 6.043}, {'end': 150.824, 'text': "and you can see here that we're running this on a duck and on a person.", 'start': 147.443, 'duration': 3.381}, {'end': 159.767, 'text': 'so if I run this code and you can see that when we pass in the duck to this function, that we get what we expect.', 'start': 150.824, 'duration': 8.943}, {'end': 161.028, 'text': 'It runs those two methods.', 'start': 159.807, 'duration': 1.221}, {'end': 169.69, 'text': "But whenever we pass the person in this function, it comes back and it didn't meet that conditional of being an instance of a duck,", 'start': 161.468, 'duration': 8.222}, {'end': 172.111, 'text': 'and instead it printed out this has to be a duck.', 'start': 169.69, 'duration': 2.421}, {'end': 180.339, 'text': "But if we remember at the beginning of the video, I said that in duck typing, we don't care if this object is actually a duck or not.", 'start': 172.651, 'duration': 7.688}, {'end': 183.823, 'text': 'We only care if it behaves like a duck when asked to do so.', 'start': 180.7, 'duration': 3.123}, {'end': 190.369, 'text': "So again, you'll see that definition online that if it walks like a duck and quacks like a duck, then treat it like a duck.", 'start': 184.303, 'duration': 6.066}, {'end': 195.273, 'text': 'So our person class does have the quack method and the fly method.', 'start': 190.729, 'duration': 4.544}, {'end': 198.935, 'text': "So there's no reason that it shouldn't work in this function also.", 'start': 195.933, 'duration': 3.002}, {'end': 204.619, 'text': "So let's just go ahead and take out these checks and just try to run it.", 'start': 199.415, 'duration': 5.204}, {'end': 211.384, 'text': "So I'm going to take out all of this code and just leave where it tries to and I'm going to leave that print statement in there to, uh,", 'start': 204.879, 'duration': 6.505}, {'end': 213.465, 'text': 'get a little space here between these two.', 'start': 211.964, 'duration': 1.501}, {'end': 219.93, 'text': "So now I'm just passing in an object and I'm just trying to run these methods and if it works, then great.", 'start': 213.906, 'duration': 6.024}, {'end': 229.798, 'text': "So now you can see that if we run this now, it doesn't care what object we're passing in, and it just comes in and it runs both of these methods,", 'start': 220.451, 'duration': 9.347}, {'end': 231.679, 'text': 'regardless of what the object is.', 'start': 229.798, 'duration': 1.881}, {'end': 233.261, 'text': "And that's what duct typing is.", 'start': 232.04, 'duration': 1.221}, {'end': 238.385, 'text': "We don't care if it's a certain type of object, we only care if it can do what we ask.", 'start': 233.681, 'duration': 4.704}, {'end': 242.528, 'text': "So now you might be thinking like well, isn't this dangerous?", 'start': 239.005, 'duration': 3.523}, {'end': 247.251, 'text': "So can't we pass in just any object that will potentially throw an error?", 'start': 242.668, 'duration': 4.583}, {'end': 248.192, 'text': "And that's true.", 'start': 247.591, 'duration': 0.601}, {'end': 257.959, 'text': 'So you might be tempted to say, okay, well, all we care about in this example is not what type of object it is, but whether it can quack and fly.', 'start': 248.752, 'duration': 9.207}, {'end': 262.543, 'text': 'So how about we put some checks in place to make sure that those methods exist?', 'start': 258.298, 'duration': 4.245}, {'end': 269.045, 'text': "So this is where that second concept comes in that it's easier to ask forgiveness than permission.", 'start': 263.143, 'duration': 5.902}, {'end': 276.948, 'text': "So first let's take a look at the non-pythonic way to do this, and this is usually called look before you leap.", 'start': 269.545, 'duration': 7.403}, {'end': 280.87, 'text': 'So let me get the snippet out here.', 'start': 277.328, 'duration': 3.542}, {'end': 288.215, 'text': "And I'm just going to go ahead and copy this code example and paste it into my function here.", 'start': 281.63, 'duration': 6.585}], 'summary': 'Duck typing in python: object behavior over type, demonstrated with quack and fly methods. non-duck-typed example and easier to ask forgiveness than permission concept shown.', 'duration': 210.765, 'max_score': 77.45, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/x3v9zMX1s4s/pics/x3v9zMX1s4s77450.jpg'}, {'end': 133.437, 'src': 'embed', 'start': 107.859, 'weight': 3, 'content': [{'end': 113.283, 'text': "And then I'm also making an instance of the class Person, and I'm passing that into our function also.", 'start': 107.859, 'duration': 5.424}, {'end': 117.707, 'text': "So first let's take a look at something that is not Duck-typed.", 'start': 113.904, 'duration': 3.803}, {'end': 119.789, 'text': 'So this is non-Pythonic.', 'start': 118.067, 'duration': 1.722}, {'end': 129.495, 'text': "So in a non-Duck-typed example, we will specifically check if the object that we're working with an instance of a duck now this one.", 'start': 120.249, 'duration': 9.246}, {'end': 133.437, 'text': 'sure that we can use all of the methods and attributes that we want to use.', 'start': 129.495, 'duration': 3.942}], 'summary': 'Demonstrating non-duck-typed example in python.', 'duration': 25.578, 'max_score': 107.859, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/x3v9zMX1s4s/pics/x3v9zMX1s4s107859.jpg'}, {'end': 288.215, 'src': 'embed', 'start': 258.298, 'weight': 2, 'content': [{'end': 262.543, 'text': 'So how about we put some checks in place to make sure that those methods exist?', 'start': 258.298, 'duration': 4.245}, {'end': 269.045, 'text': "So this is where that second concept comes in that it's easier to ask forgiveness than permission.", 'start': 263.143, 'duration': 5.902}, {'end': 276.948, 'text': "So first let's take a look at the non-pythonic way to do this, and this is usually called look before you leap.", 'start': 269.545, 'duration': 7.403}, {'end': 280.87, 'text': 'So let me get the snippet out here.', 'start': 277.328, 'duration': 3.542}, {'end': 288.215, 'text': "And I'm just going to go ahead and copy this code example and paste it into my function here.", 'start': 281.63, 'duration': 6.585}], 'summary': "Discussion on using 'ask forgiveness than permission' concept in coding.", 'duration': 29.917, 'max_score': 258.298, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/x3v9zMX1s4s/pics/x3v9zMX1s4s258298.jpg'}], 'start': 0.229, 'title': 'Pythonic coding: duck typing and eafp in python', 'summary': "Delves into the concepts of duck typing and eafp in python, advocating for clean and readable code following language conventions. it also emphasizes handling errors after attempting an action and focusing on an object's behavior rather than its type.", 'chapters': [{'end': 195.273, 'start': 0.229, 'title': 'Pythonic coding: duck typing and eafp', 'summary': "Discusses the concepts of duck typing and eafp (it's easier to ask forgiveness than permission) in python, emphasizing the importance of writing clean and readable code by following the language conventions and coding styles.", 'duration': 195.044, 'highlights': ['Duck Typing: Treating objects based on their behavior rather than their type is a key Pythonic concept, where the focus is on whether the object can perform the required actions, not on its specific type.', "Non-Duck-typed Example: Demonstrating non-Pythonic behavior by specifically checking the object's type before using its methods, which contradicts the principle of duck typing."]}, {'end': 432.577, 'start': 195.933, 'title': 'Duck typing and eafp in python', 'summary': "Discusses duck typing in python, which focuses on an object's behavior rather than its type, and the eafp (easier to ask forgiveness than permission) principle, highlighting the benefits of handling errors after attempting an action rather than checking permissions at each step.", 'duration': 236.644, 'highlights': ["Duck typing prioritizes an object's capability to perform a task over its specific type, allowing methods to run regardless of the object passed in, exemplifying a more flexible approach (e.g., regardless of the object type, both methods are executed).", "The EAFP principle encourages a more readable and efficient code by attempting to execute methods and handling errors if they occur, demonstrated through a comparison between 'look before you leap' approach and the 'EAFP' approach, showcasing the latter's advantages in simplicity and readability.", "The 'look before you leap' approach involves checking for the existence and callability of each method before execution, leading to cumbersome and permission-focused code, contrasting with the more favorable 'EAFP' approach.", "The 'EAFP' approach proves its effectiveness by successfully running existing methods and gracefully handling errors for non-existent methods, illustrating its suitability for Pythonic coding and error management."]}], 'duration': 432.348, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/x3v9zMX1s4s/pics/x3v9zMX1s4s229.jpg', 'highlights': ["Duck typing prioritizes an object's capability over its specific type, allowing methods to run regardless of the object type (e.g., both methods are executed).", "The 'EAFP' principle encourages readable and efficient code by attempting to execute methods and handling errors if they occur, showcasing its advantages in simplicity and readability.", "The 'look before you leap' approach involves checking for the existence and callability of each method before execution, leading to cumbersome and permission-focused code, contrasting with the more favorable 'EAFP' approach.", "Demonstrating non-Pythonic behavior by specifically checking the object's type before using its methods, contradicting the principle of duck typing."]}, {'end': 690.212, 'segs': [{'end': 507.617, 'src': 'embed', 'start': 479.515, 'weight': 4, 'content': [{'end': 485.14, 'text': "And if any of these keys don't exist, then we just wanna print out through the screen that we're missing some of these keys.", 'start': 479.515, 'duration': 5.625}, {'end': 493.166, 'text': 'So you might be tempted to do it like this, which is the look before you leap or the asking permission, which is the non-pythonic way.', 'start': 485.7, 'duration': 7.466}, {'end': 498.59, 'text': 'We might be tempted to say do we have this name key in this person??', 'start': 493.586, 'duration': 5.004}, {'end': 500.451, 'text': 'Do we have this age key??', 'start': 499.21, 'duration': 1.241}, {'end': 501.812, 'text': 'Do we have this job key??', 'start': 500.632, 'duration': 1.18}, {'end': 507.617, 'text': 'If all those exist, then print out this sentence that prints out the name, the age and the job.', 'start': 502.173, 'duration': 5.444}], 'summary': 'Check for existence of keys and print missing ones, use pythonic approach for conditional printing.', 'duration': 28.102, 'max_score': 479.515, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/x3v9zMX1s4s/pics/x3v9zMX1s4s479515.jpg'}, {'end': 547.695, 'src': 'embed', 'start': 521.352, 'weight': 0, 'content': [{'end': 528.32, 'text': "Now let's compare this example to the concept that it's easier to ask forgiveness than permission.", 'start': 521.352, 'duration': 6.968}, {'end': 535.226, 'text': "So I'm going to comment out the look before you leap code here, and I'm going to uncomment out this code here.", 'start': 528.661, 'duration': 6.565}, {'end': 538.128, 'text': "Now what we're saying, we're not making any checks at all.", 'start': 535.647, 'duration': 2.481}, {'end': 543.713, 'text': "We're just saying, hey, try to print out this sentence that accesses all these different keys.", 'start': 538.369, 'duration': 5.344}, {'end': 547.695, 'text': "If it doesn't work, throw a key error and say that we're missing a key.", 'start': 544.133, 'duration': 3.562}], 'summary': "Comparing 'ask forgiveness than permission' approach in coding, omitting checks and throwing key error if missing", 'duration': 26.343, 'max_score': 521.352, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/x3v9zMX1s4s/pics/x3v9zMX1s4s521352.jpg'}, {'end': 595.62, 'src': 'embed', 'start': 566.266, 'weight': 2, 'content': [{'end': 573.11, 'text': "We're not making all of these checks saying, can we do this? Can we do this? And can we do this? We're just trying to do it.", 'start': 566.266, 'duration': 6.844}, {'end': 574.871, 'text': "And if it works, then that's great.", 'start': 573.49, 'duration': 1.381}, {'end': 579.594, 'text': "And if it doesn't work, then we print out the error or handle it in any way that we want to handle it.", 'start': 575.071, 'duration': 4.523}, {'end': 587.857, 'text': 'Now just another small example here, if I grab another snippet from the list, we can also do this with list.', 'start': 580.074, 'duration': 7.783}, {'end': 595.62, 'text': 'So if I paste this in here and let me uncomment out this code, and actually I forgot to actually grab the list here,', 'start': 588.037, 'duration': 7.583}], 'summary': 'Trial and error approach taken for implementing code, handling errors as needed.', 'duration': 29.354, 'max_score': 566.266, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/x3v9zMX1s4s/pics/x3v9zMX1s4s566266.jpg'}, {'end': 639.794, 'src': 'embed', 'start': 612.922, 'weight': 1, 'content': [{'end': 617.267, 'text': 'so What you might be tempted to do is check the length of the list.', 'start': 612.922, 'duration': 4.345}, {'end': 627.158, 'text': 'You might say, if my length of my list is greater than or equal to six, then I know that I can access this fifth index here.', 'start': 617.548, 'duration': 9.61}, {'end': 632.283, 'text': "And if it's not greater than that, then I need to print that this index doesn't exist.", 'start': 627.658, 'duration': 4.625}, {'end': 639.794, 'text': 'So you can see that if I run this, then we did print out that fifth index of the list.', 'start': 632.904, 'duration': 6.89}], 'summary': 'Using conditional logic to access list index, demonstrates accessing fifth index if list length is greater than or equal to six.', 'duration': 26.872, 'max_score': 612.922, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/x3v9zMX1s4s/pics/x3v9zMX1s4s612922.jpg'}, {'end': 690.212, 'src': 'embed', 'start': 664.375, 'weight': 3, 'content': [{'end': 675.501, 'text': "So if we just do try to print this index and then just throw the exception index error if it's an index error and print that that index doesn't exist?", 'start': 664.375, 'duration': 11.126}, {'end': 682.006, 'text': 'So now, if I run this, you can see that we printed out the index of that list.', 'start': 676.061, 'duration': 5.945}, {'end': 690.212, 'text': 'but if we were to take this 6 out of here and try to run it, then we just throw this index error and print that the index does not exist.', 'start': 682.006, 'duration': 8.206}], 'summary': "Code prints index of list; throws index error if index doesn't exist.", 'duration': 25.837, 'max_score': 664.375, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/x3v9zMX1s4s/pics/x3v9zMX1s4s664375.jpg'}], 'start': 433.118, 'title': 'Pythonic approaches in python', 'summary': "Discusses pythonic dictionary handling, including 'look before you leap' and 'easier to ask forgiveness than permission' approaches, and demonstrates the pythonic way of handling index errors using try-except blocks with practical examples.", 'chapters': [{'end': 566.246, 'start': 433.118, 'title': 'Pythonic dictionary handling', 'summary': "Discusses two approaches for handling dictionary keys in python: 'look before you leap' and 'easier to ask forgiveness than permission'. the former involves checking if keys exist before accessing them, while the latter involves accessing keys directly and handling errors if they don't exist.", 'duration': 133.128, 'highlights': ["The chapter discusses two approaches for handling dictionary keys in Python: 'look before you leap' and 'easier to ask forgiveness than permission'.", "The former involves checking if keys exist before accessing them, while the latter involves accessing keys directly and handling errors if they don't exist.", "It said that we're missing some keys.", "If it doesn't work, throw a key error and say that we're missing a key."]}, {'end': 690.212, 'start': 566.266, 'title': 'Handling index errors in python', 'summary': 'Discusses handling index errors in python, demonstrating the non-pythonic way of checking list length for index access and the pythonic way of handling index errors using try-except blocks, showcasing the effectiveness of the latter approach with practical examples.', 'duration': 123.946, 'highlights': ['The chapter emphasizes the non-Pythonic way of checking list length for index access, illustrating the ugliness of the approach and the necessity of a more elegant solution.', 'It showcases the Pythonic way of handling index errors using try-except blocks, demonstrating the simplicity and effectiveness of this approach in gracefully managing index errors within code.', 'The demonstration includes practical examples of accessing list indices, showcasing the effectiveness of the try-except approach in handling index errors and providing a more elegant and Pythonic solution.', 'The chapter highlights the importance of adopting Pythonic practices in code, emphasizing the elegance and effectiveness of the try-except approach in handling potential index errors.']}], 'duration': 257.094, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/x3v9zMX1s4s/pics/x3v9zMX1s4s433118.jpg', 'highlights': ["The chapter discusses two approaches for handling dictionary keys in Python: 'look before you leap' and 'easier to ask forgiveness than permission'.", 'The chapter emphasizes the non-Pythonic way of checking list length for index access, illustrating the ugliness of the approach and the necessity of a more elegant solution.', 'The demonstration includes practical examples of accessing list indices, showcasing the effectiveness of the try-except approach in handling index errors and providing a more elegant and Pythonic solution.', 'The chapter highlights the importance of adopting Pythonic practices in code, emphasizing the elegance and effectiveness of the try-except approach in handling potential index errors.', "The former involves checking if keys exist before accessing them, while the latter involves accessing keys directly and handling errors if they don't exist."]}, {'end': 915.318, 'segs': [{'end': 746.365, 'src': 'embed', 'start': 690.793, 'weight': 3, 'content': [{'end': 692.714, 'text': 'So again, this goes back to that concept.', 'start': 690.793, 'duration': 1.921}, {'end': 695.256, 'text': "We're not asking for permission that we can do something.", 'start': 692.934, 'duration': 2.322}, {'end': 699.64, 'text': "We're just trying to do it, and if we can't, then we'll handle it the way that we want to handle it.", 'start': 695.597, 'duration': 4.043}, {'end': 704.143, 'text': "Now, I also want to point out that this isn't an end-all be-all approach.", 'start': 700.04, 'duration': 4.103}, {'end': 711.008, 'text': 'There have been some situations online where people have pointed out where you might want to make certain checks in certain situations.', 'start': 704.483, 'duration': 6.525}, {'end': 713.39, 'text': 'But why is this?', 'start': 711.489, 'duration': 1.901}, {'end': 715.192, 'text': 'easier to ask forgiveness than permission?', 'start': 713.39, 'duration': 1.802}, {'end': 719.235, 'text': 'Why is that even considered a good thing to do in most cases??', 'start': 715.872, 'duration': 3.363}, {'end': 729.518, 'text': "Well, first of all, it's slightly faster in situations where you don't expect a lot of exceptions, because whenever you ask for permission,", 'start': 719.815, 'duration': 9.703}, {'end': 732.959, 'text': 'you have to access your objects multiple times.', 'start': 729.518, 'duration': 3.441}, {'end': 738.101, 'text': 'But here, we just try to access it one time, and if it works, then it works.', 'start': 733.379, 'duration': 4.722}, {'end': 740.882, 'text': "So we're only accessing that object once.", 'start': 738.581, 'duration': 2.301}, {'end': 746.365, 'text': 'now. another reason is that some people make the argument that it is more readable.', 'start': 741.562, 'duration': 4.803}], 'summary': "Advocating for 'easier to ask forgiveness than permission' approach, citing faster access and improved readability.", 'duration': 55.572, 'max_score': 690.793, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/x3v9zMX1s4s/pics/x3v9zMX1s4s690793.jpg'}, {'end': 861.271, 'src': 'heatmap', 'start': 828.726, 'weight': 2, 'content': [{'end': 839.911, 'text': 'the reason that this is a race condition is because when we make this check here to see if we can access the file by the time we get down here in that short amount of time,', 'start': 828.726, 'duration': 11.185}, {'end': 841.992, 'text': "maybe we can't access the file anymore.", 'start': 839.911, 'duration': 2.081}, {'end': 845.79, 'text': "So then when we try to open it, then we're going to throw an error.", 'start': 842.392, 'duration': 3.398}, {'end': 850.136, 'text': "And we're likely not going to catch that error because we thought that we could access the file.", 'start': 846.15, 'duration': 3.986}, {'end': 858.347, 'text': 'So really, the way to do this, which uses that same concept of asking for forgiveness and not permission.', 'start': 850.957, 'duration': 7.39}, {'end': 861.271, 'text': 'So let me comment out this code here.', 'start': 859.308, 'duration': 1.963}], 'summary': 'Race condition occurs if file access check and file opening are not synchronized.', 'duration': 21.36, 'max_score': 828.726, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/x3v9zMX1s4s/pics/x3v9zMX1s4s828726.jpg'}, {'end': 912.016, 'src': 'embed', 'start': 884.939, 'weight': 0, 'content': [{'end': 888.2, 'text': 'And if not, then we catch that error and handle it as we want.', 'start': 884.939, 'duration': 3.261}, {'end': 890.501, 'text': "So I think that's going to do it for this video.", 'start': 888.9, 'duration': 1.601}, {'end': 897.606, 'text': "I hope that it gave you a better understanding of duck typing and the concept that it's easier to ask forgiveness than permission.", 'start': 891.022, 'duration': 6.584}, {'end': 904.371, 'text': "So these are two very common examples that I see online when someone points out that someone's code isn't Pythonic.", 'start': 898.246, 'duration': 6.125}, {'end': 908.073, 'text': 'So I wanted you to be aware of what those concepts meant exactly.', 'start': 904.491, 'duration': 3.582}, {'end': 912.016, 'text': 'But if you do have any questions, just feel free to ask in the comment section below.', 'start': 908.433, 'duration': 3.583}], 'summary': 'Explained duck typing and easier to ask forgiveness than permission. common examples of non-pythonic code addressed.', 'duration': 27.077, 'max_score': 884.939, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/x3v9zMX1s4s/pics/x3v9zMX1s4s884939.jpg'}], 'start': 690.793, 'title': 'Decision-making mindset and pythonic code', 'summary': "Explores 'easier to ask forgiveness than permission' mindset in decision-making and discusses the advantages of 'asking for forgiveness, not permission' in python, including faster execution, improved readability, and mitigation of race conditions.", 'chapters': [{'end': 719.235, 'start': 690.793, 'title': 'Forgiveness vs permission', 'summary': "Explores the concept of 'easier to ask forgiveness than permission' in decision-making, emphasizing the approach of taking action without seeking permission and addressing the implications of this mindset.", 'duration': 28.442, 'highlights': ["The concept of 'easier to ask forgiveness than permission' is discussed, highlighting the approach of taking action without seeking permission and addressing the implications of this mindset.", 'The speaker emphasizes the importance of handling situations in the desired manner if permission is not granted, reinforcing the notion of taking initiative and responsibility.', 'The chapter delves into the consideration of why it is often viewed as favorable to ask forgiveness rather than permission in decision-making, prompting a critical analysis of this approach.']}, {'end': 915.318, 'start': 719.815, 'title': 'Duck typing and pythonic code', 'summary': "Discusses the concept of 'asking for forgiveness, not permission' in python, highlighting its advantages such as faster execution, improved readability, and mitigation of race conditions, using examples and python documentation.", 'duration': 195.503, 'highlights': ["The concept of 'asking for forgiveness, not permission' in Python allows for faster execution by accessing objects only once, improving readability, and mitigating race conditions (e.g., avoiding potential race conditions when checking file access).", "Using 'asking for forgiveness, not permission' can lead to faster execution in situations where multiple object access is required, as it eliminates the need for repeated permission checks, thus reducing overhead and improving performance.", "Adopting the 'asking for forgiveness, not permission' approach in Python can improve code readability and reduce the need for excessive condition checks, making the code more concise and clear.", "Implementing the 'asking for forgiveness, not permission' approach can mitigate race conditions in certain situations, as demonstrated through an example from the Python documentation, where it helps avoid potential race conditions when checking file access."]}], 'duration': 224.525, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/x3v9zMX1s4s/pics/x3v9zMX1s4s690793.jpg', 'highlights': ["The concept of 'asking for forgiveness, not permission' in Python allows for faster execution by accessing objects only once, improving readability, and mitigating race conditions (e.g., avoiding potential race conditions when checking file access).", "Adopting the 'asking for forgiveness, not permission' approach in Python can improve code readability and reduce the need for excessive condition checks, making the code more concise and clear.", "Implementing the 'asking for forgiveness, not permission' approach can mitigate race conditions in certain situations, as demonstrated through an example from the Python documentation, where it helps avoid potential race conditions when checking file access.", "The concept of 'easier to ask forgiveness than permission' is discussed, highlighting the approach of taking action without seeking permission and addressing the implications of this mindset.", 'The speaker emphasizes the importance of handling situations in the desired manner if permission is not granted, reinforcing the notion of taking initiative and responsibility.', 'The chapter delves into the consideration of why it is often viewed as favorable to ask forgiveness rather than permission in decision-making, prompting a critical analysis of this approach.', "Using 'asking for forgiveness, not permission' can lead to faster execution in situations where multiple object access is required, as it eliminates the need for repeated permission checks, thus reducing overhead and improving performance."]}], 'highlights': ["Duck typing prioritizes an object's capability over its specific type, allowing methods to run regardless of the object type (e.g., both methods are executed).", "The 'EAFP' principle encourages readable and efficient code by attempting to execute methods and handling errors if they occur, showcasing its advantages in simplicity and readability.", "The concept of 'asking for forgiveness, not permission' in Python allows for faster execution by accessing objects only once, improving readability, and mitigating race conditions (e.g., avoiding potential race conditions when checking file access).", "The chapter discusses two approaches for handling dictionary keys in Python: 'look before you leap' and 'easier to ask forgiveness than permission'.", 'The chapter emphasizes the non-Pythonic way of checking list length for index access, illustrating the ugliness of the approach and the necessity of a more elegant solution.', "Using 'asking for forgiveness, not permission' can lead to faster execution in situations where multiple object access is required, as it eliminates the need for repeated permission checks, thus reducing overhead and improving performance.", 'The chapter highlights the importance of adopting Pythonic practices in code, emphasizing the elegance and effectiveness of the try-except approach in handling potential index errors.', "Adopting the 'asking for forgiveness, not permission' approach in Python can improve code readability and reduce the need for excessive condition checks, making the code more concise and clear.", "The 'look before you leap' approach involves checking for the existence and callability of each method before execution, leading to cumbersome and permission-focused code, contrasting with the more favorable 'EAFP' approach.", "The concept of 'easier to ask forgiveness than permission' is discussed, highlighting the approach of taking action without seeking permission and addressing the implications of this mindset."]}