Knowing this can save you HOURS of debugging! (5 Python Pitfalls)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
have you ever spent hours of debugging trying to find an annoying bug and then realized it was a very silly mistake in the end i think we've all been there so in this little video i want to show you five python nitpicks that can save you a lot of time once you are aware of them so let's jump right into it the first one involves a string feature not many people know so let's start by creating a list of strings so let's put some elements into this one two three four and also five and now let's first of all print the length of our list so print length of my list so now if i run this you would expect five and now let's let's see what happens so we actually only see four elements in it so what's happening here and if you are a close watcher then you might have already noticed that i missed a comma here after these elements so let's put these into separate lines to make this clearer and now you see that here a comma is missing so what's happening inside so let's print the list and inspect the elements and then you see we have one element and now this string here got combined and that's why we only see four elements in our list and yeah this is the string feature not a lot of people know so if i create a string by saying hello and then just a space and another word then it actually just combined these two strings into one so if i print this then we see hello world so this works the same as just with a plus sign and yeah this works and i wish that python would throw a syntax error here but it does not so yeah this is a common mistake a lot of people don't know that if you just use two strings after each other then this works the second one involves multiple assignments and the conditional expression so as you might know we can do something like this we can say x and y equals one and zero in one line and now if we run this we see x is one and y is zero so now let's also use a conditional assignment so let's say we have a condition that is true so we somehow computed this in our code and then we say if our condition is true x and y should be 1 and 0 and otherwise we want to say x and y equals none and none and now if we run this then our condition is true so we see 1 and 0 and now if we change our condition to false then we see none and none so here we see it and now let's actually also do this in one line with the conditional assignment so we can say x and y equals one and zero if our condition and otherwise we want to say this is none and none so this is essentially the same logic as this so now let's remove this and let's print this so our condition is still false so we expect to see none and none and let's run this and yeah it's none and none so let's change this condition back to true and now we expect to see one and zero so now we actually see three elements one and zero and also none so now what's happening here is that let's first of all print only x and then we see x is actually a tuple now of one and zero so now what happened here is that we use parentheses for the first element because we thought this would improve the readability of our logic but now it actually messed up the order so now what happens here is that the logic is x is 1 and 0 if the condition is true and otherwise x is none and y is always none so that's why we see this for our x and y is always none so now to actually get what we expected we also have to use parentheses here so now if we print x then we see x is only one and if we also print y then we see our y is zero so yeah be aware that if you do it like this then use consistent parentheses for both conditions but for this use case i actually recommend to use the long version with the normal if else statement then it's less likely that you make any errors the next one involves tuple creation so let's create a tuple with parentheses and then put in multiple elements so two strings in this case and now let's loop over this four x in t and then we want to print the element x and now if we run this then we see one and two now let's create an empty tuple with empty parentheses and run it again then we don't see anything and now let's only put in one element one and now if we run this then we expect to see one but what happened is we actually now see three lines and with one character each so what's happening here first of all let's actually print the type of our tuple to see what this is and we see this is a string so this is not a tuple now so this is not a tuple constructor so if you want to create a tuple with only one element then we need a comma at the end so now if we run it again then it is what we expected so yeah be aware that if you want to create a tuple with only one element then you need a comma here at the end also you might think that you can create it like this with the explicit tuple function so now if we run this then we still see class two pull but we again have three elements in it so now if we print the actual tuple then we see we have three elements with one character each so if you pass a string to the tuple constructor then it creates a tuple with one character for each element so yeah be aware that this can be tricky when you want to create a tuple the next one involves the assert statement so in python you can say assert and then a condition and then let's also print something and the assert statement works like this so if this is true and we run this then nothing happens so we simply see our print statement and if the condition is false and we run this then we get the assertion error so this can be used in your code where you always expect this condition to be true and otherwise you can catch this as a bug and then you can also use a comma and a optional custom error message so my custom error so now if we run this again then we see we also see our custom error message so the syntax is actually pretty simple and now let's check this for an actual condition let's say a equals python and let's say b equals another string javascript and then we always expect these strings to be the same so let's say we do a cert a equals equals b and otherwise we throw the error the languages are not the same so now if we run this then we expect to get an assertion error because obviously these are not the same strings so now we don't get the assertion error but at least we get the syntax warning assertion is always true perhaps remove parentheses so what happens here is that since we use parentheses this is seen as a tuple with two elements one boolean and one string and since this is not an empty tuple this is evaluated to true so this is always true so in order to get what we expected we actually have to remove the parentheses and now if we run this then we get the assertion error the languages are not the same so yeah keep that in mind that for the assert statement you should not use parentheses around your logic and the last one is probably obvious but it still happens sometimes that let's say you have a list with some elements one two and three and then you want to update this by saying my list equals my list dot append and then another item for and now if we print this and then we expect to see one two three and four and now we actually only see none and this is because obviously append is an in-place operation that does return none as a result so we simply have to say my list dot append and then we get what we expect and the same is true for example for the sort function if we say my list dot sort then it sorts the list and again this is an in place operation that simply returns none as a function so if we do it like this then again we get none and the same for example is true for dictionaries and the update function this is also an in place operation so yeah keep that in mind that if you want to do this then don't assign it back to the list all right that's it i hope you enjoyed this and maybe learned something new and before i end this video i also want to give full credits to these guys from this github repository it's called what the python and in there you find a lot of strange code snippets with python pitfalls like the one i showed you and i recommend to check this out it's really fun and then i hope to see you in the next video bye
Info
Channel: Python Engineer
Views: 9,681
Rating: 4.8611112 out of 5
Keywords: Python
Id: 8zbXyd_tA9A
Channel Id: undefined
Length: 10min 45sec (645 seconds)
Published: Sun Sep 05 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.