5 Things You're Doing Wrong When Programming in Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Could you please list out the 5 mistakes? I don't want to watch a 12 minute video if I already know what those 5 mistakes are. Or if I know 4 of the 5, I'd like to skip to the one I don't know.

Thanks.

πŸ‘οΈŽ︎ 194 πŸ‘€οΈŽ︎ u/RangerPretzel πŸ“…οΈŽ︎ Feb 06 2020 πŸ—«︎ replies

Mostly good stuff, but highly disagree with number 3. Sending your caught exception to a variable is a good method, because then you can log/process it however you want.

πŸ‘οΈŽ︎ 20 πŸ‘€οΈŽ︎ u/[deleted] πŸ“…οΈŽ︎ Feb 06 2020 πŸ—«︎ replies

In the last solution, you’re type checking as

if isintance(var, type(None))

Why not just take advantage of the fact None is a singleton and simply do if var is None ?

πŸ‘οΈŽ︎ 21 πŸ‘€οΈŽ︎ u/chromium52 πŸ“…οΈŽ︎ Feb 06 2020 πŸ—«︎ replies

surprisingly this is actually useful content. I need to use more sets in my coding.

πŸ‘οΈŽ︎ 16 πŸ‘€οΈŽ︎ u/humanitysucks999 πŸ“…οΈŽ︎ Feb 06 2020 πŸ—«︎ replies

Had no idea about the mutable default args, scary stuff.

πŸ‘οΈŽ︎ 7 πŸ‘€οΈŽ︎ u/OPKatten πŸ“…οΈŽ︎ Feb 06 2020 πŸ—«︎ replies

Great video. Just subscribed to your channel!

πŸ‘οΈŽ︎ 4 πŸ‘€οΈŽ︎ u/gabrielmotaaa πŸ“…οΈŽ︎ Feb 06 2020 πŸ—«︎ replies

Thanks

πŸ‘οΈŽ︎ 2 πŸ‘€οΈŽ︎ u/Mitsu11 πŸ“…οΈŽ︎ Feb 06 2020 πŸ—«︎ replies

Well I’m not on the level yet to understand that but i will definitely save that for later.

πŸ‘οΈŽ︎ 2 πŸ‘€οΈŽ︎ u/MichaellZ πŸ“…οΈŽ︎ Feb 06 2020 πŸ—«︎ replies

Ok

πŸ‘οΈŽ︎ 2 πŸ‘€οΈŽ︎ u/mercedezbeans πŸ“…οΈŽ︎ Feb 06 2020 πŸ—«︎ replies
Captions
so in the four or so years that I've been using Python and been getting paid for it I have seen a lot of beginners make fairly large number of mistakes while writing Python code and I have made almost all of those mistakes myself as well as when I was starting out so in this video I want to cover five of the mistakes that I see a fair amount and don't necessarily see talked about too much so let's get started the first mistake that I want to go over is when people don't use the if name equals main construct in their scripts now to demonstrate what this means I have set up this very simple script that has this one function it is an incredibly useful function as you can tell by its name and what it does is in a loop five times in a row it swears to us that it is incredibly incredibly useful and then to take full advantage of this function on the bottom here we call this function and then we can just run it as a Python script and you know it swears repeatedly to us that it is very very useful indeed now this is all great and whatnot but since this function is so useful we might want to use it in a different script or in a library or something like that if we want to do that we would want to import this function somewhere so let's go into an eye Python session and try that so from one main import useful function and when we do that the import actually causes this function to run before it returns this of course is happening because when we do the import Python executes the scripts to make sure that the function gets loaded and everything and as it's doing that it gets to line eight and the function gets called there is a construct in Python to avoid doing this we can just go ahead and comment this line out and uncomment these two lines below where I have if dunder name equals a string which is just dunder main this particular conditional is only ever going to be true if we are running one underscore main as a script if we're doing an import this is not going to be true and that is something that we can demonstrate now by running the script first just to make sure that it is actually doing what we wanted to do and then dropping into an eye Python session and doing the import again and now if the import works as expected and of course we can also call this function as well okay let's move on to mistake number two mistake number two has to do with exceptions I have set up another very simple script so this script is also relatively straightforward it is an infinite loop and inside this infinite loop we have a try and accept block like we often tend to do we don't want this loop to ever really crash we want it to continue doing what it's doing so that's why in a try we do some really useful things and then we have an accept statement just in case somehow this print and time dot sleep statements raise some sort of an exception you never know in this accept block we print that something bad happened but then continue going now this is great when you want to just run this script and you want the script to continue running despite any internal exceptions coming up but let's say you're running the script and things are going great and now you want control if you're terminal back so you try to kill it with a control C and you continue trying to kill it and nothing happens so what's happening every time we hit control C we get this message ow you know whatever I'm gonna keep running this is because the Python interpreter is catching the control C signal and raising that as an error now that's not a standard exception but when you have a bear accept like this any errors that are raised are going to be caught in it and the keyboard interrupt error is also going to be caught that's why now we can't really stop the script unless we do something like control backslash which is not very clean now to fix this as you may see the pattern in all these examples there's some commented code on the bottom that actually fixes this then the fix of this one is really really straightforward instead of using a bear except we do the bare minimum in this case and make sure that we are accepting at least all the exceptions that subclass from exception and keyboard interrupt happens to not be one of them now if we go ahead and run this script it works just as expected but if we do control C you'll see over here it's raising the keyboard interrupt error which then does not get caught in this except statement we can also test that other exceptions will be caught by raising an exception here and yeah now we can see that the exception is being raised and it's being caught everything is as we expect it to be and we can also control see out of the script this particular mistake can become really really annoying really really fast for beginners because they keep not being able to stop their scripts and they're like why is this happening what is going wrong and it's just almost always a bear except keeping in line with theme of exceptions let's talk about something that isn't quite an error but can lead to some annoyance especially when people are trying to debug an error that may have happened in a production system or an automated system somewhere I don't exactly know how common this is but when I started my first job everybody would do this where they would write try and accept blocks in this fashion where in the try block if an exception is raised with an except they weren't using Bare exceptions but then they were binding the exception that was being raised to a variable e and then inside it printing that variable or sending it to some logging system this is not very good and I'll show you why if we go to the terminal on the left and run this script what you'll notice on the top here is that if we simply print E we get whatever the message there was that was associated with the exception now oftentimes things that are in the standard library are gonna raise a very useful error message but things that may have been written by let's say me or a number of other library authors the exceptions can sometimes have really really terrible messages so that doesn't really help you do a lot with it on the other hand if you look at all the stuff that got printed below here that's the stuff that we want not normally if you run a script and it causes an exception and you're not catching that exception the script is going to crash and it'll it's going to show you a trace back in this case we don't want the script to crash but we also would want to see I would want to see the trace back just so I can go back and figure out where exactly the error occurred and how to fix it now there are two ways that you can get the trace back here like I'm showing printed over here both of them come from the trace back module one of these ways is to straight-up print the trace back to standard out and that you can do by calling the print exe function under the trace back module the second one is using the format exe function and that gives you a string representation off the trace back which you can either print out or send to a logging system or save into a file or do whatever you want with it it is worth noting that if you have logging setup correctly or if you're using some external logging system like sentry they do these things are automatically and save the trace back but in cases where you have to be manual for whatever reason or if you're just being lazy at the very least you should use the trace back module and get the full trace back ok let's leave the land of exceptions and go to the land of lists now this one isn't quite a Python mistake per se this can happen in any language but I feel like the in the construct in Python makes it easy for it to happen in Python basically what this comes down to is that I've seen a lot of people check for membership off some number or string usually it's a string because usually it's a path or something like that in a very long list those of you that know your big o-notation will know that that is an order N or linear time operation and that is not very good when the list becomes really really large and you're doing a lot of membership checks so to simulate that I've created this file that has lots of names and then I'm reading them into an array and it has 1 million members and then I'm also creating a set for this because I want to show the better way of doing this I've set up two functions one is called in names and the other one is called in name set and both of these functions are running a loop a hundred times and then just doing something that checks to see if a string exists inside either names or names set and then I'm timing them on the bottom now if I go ahead and run this script you'll see that the first function in names took a good 2.52 seconds in the second function which accomplished the exact same thing took on the order of microseconds and if we examine these functions they look identical we're just checking for the membership of some string in either the array or the set but when you check this membership in a set this can also be a hash table by the way that's an order 1 operation whereas checking it against the list is at worst an order n operation so yeah if you have situations like this where you're maybe checking to see if a file is something that you've already seen based on its name and you have some sort of an aggregation data structure don't let that data structure be a list make that into a set or a hash table so that you can do order 1 lookups and if it's not already a setter a hash table converted into that because you can pay that cost the first time and then not pay it again repeatedly as you're doing this membership checks and then continuing on with the theme of lists let's talk about our last mistake for this video which is when you create a default argument to a function that is a mutable type so to demonstrate what I mean I have set up a fairly straightforward function I don't know if you would ever use a construct like this in practice but this is good for a demonstration the function just takes two arguments one of them is a list of names and the other one is some list where we're aggregating these names the function does something very simple it just goes over all the names it takes in whatever the aggregate list was passed up ends the new names to it and then returns that list now we want to make the API of this function friendly so we give the aggregate list argument a default value and that default value happens to be empty list on the bottom here I have put in a very simple test for this function where I'm calling it twice I'm passing it the same array of names and and I'm printing out the result now we would expect this to print out the same thing twice right okay what's happening this is really odd instead of printing the same thing twice it's printing that list and then it's printing that list kind of doubled up so effectively what's happening is that when you define this function and python loads it into memory it creates a new empty list and then it assigns a pointer to that to this named aggregate list this is where everything goes wrong because when we try to use the default value for aggregate list it is the exact same list it is always pointing back to the same area in memory so we just keep adding things to that list this is gonna happen whenever you have a mutable type like this it's only ever going to get initialized once when the function is first loaded into memory and then it's never gonna get recreated when you call the function without that argument this is not something that we normally experience when we have arguments that our floats or int sore bowls and that's because these arguments are not mutable but lists are and your custom data types can also be so this can very easily turn into a very very subtle that becomes extremely difficult to diagnose but given the theme of this video we do have a solution the solution looks a little bit ugly but a little bit of ugly here will save you in the long run the only change that we make is instead of setting this argument to an empty list we set it to none and then check inside the function to see if a nun was passed and if it was we initialize a new array now if we go ahead and run this script we get the output just as we expected now of course you might not want to do this aggregate list equals nun thing and that's perfectly fine but in this case maybe it's okay for you to have the user do a little bit more work and not create a as I said nice API alright and that'll do it for this video thank you very much for watching if you liked it please leave a like if you haven't subscribed yet please consider subscribing and also hitting the bell icon so that you always know when I upload a new video I have more content in the pipeline for Python space Macs and machine learning and reinforcement learning related stuff so stay tuned thanks for watching bye
Info
Channel: Jack of Some
Views: 384,724
Rating: 4.9373565 out of 5
Keywords: python coding wrong, python coding mistakes, python programming mistake, 10 programming mistakes, stop making these mistakes in python, python programming mistakes, python programming wrong, 5 python mistakes, python mistakes, python tutorial, python, programming
Id: fMRzuwlqfzs
Channel Id: undefined
Length: 12min 2sec (722 seconds)
Published: Wed Feb 05 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.