6 CLEAN Tips To IMPROVE Your Python Functions

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to be going over six tips that can help you improve the readability and the practicality of your functions in Python now for the first tip I always recommend when you're creating a function to specify the return type it saves you a lot of trouble later when you get unexpected surprises because if you do not specify a return type if you return something such as a string for example you will never know that you're returning a string and that that is wrong sometimes that's going to happen you're going to have the wrong data type being returned so it's worth the trouble to give it that type annotation because it will warn us ahead of time that we're doing something wrong here and we're returning a data type that we did not want to return such as string and if we catch that early on we can of course change it to the data type that it should be and that will save us some trouble later on when we are working on our program now the second tip isn't really that different from the first tip suppose you have a function and that function takes some numbers and it's going to return a list of integer inside here we want to reverse the numbers for whatever reason we type in numbers dot reverse but you might notice immediately that we're not getting any context options and that can be quite annoying because that can lead us to typing in code that might not actually make sense and it's just always nice when our code editor gives us those context options but regardless we're going to return the numbers at the bottom and everything's going to work perfectly fine what I suggest you do in this situation is giving the parameter a type hint as well so if you want to take a list of integer put it inside that so that the program knows because it does come with the benefit of giving us the context options which can make it very easy for us to pick how we want to edit this code and can just make our workflow a lot more smooth so I personally love being explicit with the data types I'm using some people might not I personally recommend it for the third tip pretend you have a lot of arguments that you want to include inside your function maybe you have a sum function and it's not always convenient to take a list especially if you're doing some instant calculations you might want to use the asterisk args instead so to do this we'll type in some numbers and here we'll type in asterisk args and that allows us to insert an infinite or indefinite amount of arguments but to make this more readable I'll type in numbers and that's going to be of type floats and it's going to return to us a float then inside here we can just type in return these sum of the numbers and python already has an inbuilt function for returning the sum of numbers but only from an iterable so the benefit of using our function is that we can type in some numbers and we can do one two three four five and we don't have to pass it in as a list we can just pass it in as it is and it's not going to do anything because we didn't print it but if we print these numbers we're going to get 15 as a return and we were able to insert as many arguments as we wanted without having to specify all of those arguments inside the Constructor so now we have 45 as a return and if you're not familiar with this syntax all it's doing is unpacking all the arguments that we put inside here and turning them into a tuple so if we actually print the numbers right here you'll see that we will get the Tuple back inside our function along with the result of the numbers so that's just a cool tip for speeding up your workflow one place that this is actually being used every day is in the print statement here you can print 10 10 15 hello and so on because this takes args which means if we run this it's going to do the exact same thing tip number four has to do with keeping your code simple now in general you want every function to be as simple as possible it should have one job at most maybe two if you're pushing it but in general you want your functions to do one thing so that you can reuse them so that you can reuse them as much as possible now I'm going to show you two different approaches to creating this make payment function so here I pasted in the desired version we have two functions to make the payment first we need to be able to accept the payment so we have the code for accepting the payment here we check if the balance is more than zero and if it has the balance we can process it otherwise we can deny it and we're going to return has balance so that we can use this in a different function such as the make payment function now for the make payment we're going to determine that it was a success if the payment was accepted so if it was a success we're going to process it and we're going to say payment succeeded else we're going to say payment failed and if we run this since the value is more than zero the payment was accepted and the payment succeeded so we split this up into two different functions to make sure that it was simple and that we can accept the payment from any function that we decide to actually use this with a sloppy approach would be to put both of these into a single function because now we are mixing up a lot of code together and that means if we have to ever reuse any of these components we're going to have a lot of trouble on our hands and it can also make editing this code a bit more difficult this was a very simple example of course as you can see I only have hash code to fill in some blocks but once your functions become more complex it is just a highly respected practice to keep them as simple as possible if you have a function that makes a payment from a credit card for example you want to have all those functions separated because you're going to be able to reuse those and edit those much more conveniently so again instead of having all the checks in one single function split that up into simple Parts you're going to appreciate it later on when you're coding much bigger projects tip number five now suppose you have a function called Adder it might seem quite straightforward what you have to do here but for people using your code they might want that extra documentation and for example if you type in print and you hover over print you're going to get this nice documentation so one huge tip for improving the readability of your function is to add doc strings and this will help everyone add a concise doc string of what the function does include what the parameters do if they're not that clear and everyone's going to be happy because no matter how obvious it might seem in the future someone's going to have to learn it and if they have to learn it by testing out a million different scenarios that's going to lead to a huge loss in time so so just by double tapping enter in pycharm it's even going to give us some special keywords such as what the parameters are and here you can Define what it does you can see numbers to be added together and it Returns the total now if we type in adder and we hover over it we're going to get this in the documentation and this is cool but for the minimum you should at least give it a short line that explains what the function does if it's quite obvious what the function does I wouldn't recommend writing it in the docstring write something that's more informative than what the function actually says and now if we hover over Adder we're going to get this really nice documentation that tells us exactly what the function does what it returns and so on and this probably wasn't the best example you could say Returns the total as a floats so keep it descriptive keep it concise and keep it informative and other developers will love you for the final tip I decided to include a global in our function which I absolutely recommend you avoid as much as possible because this really makes our code dependent on the current module and this can have a lot of unexpected side effects when you are working with this function now anyone can access this string and this function is glued to this string which makes it very inconvenient in terms of reusability when it comes to structuring our program especially if we want to use this function in a different module it just becomes a mess now a better solution is just to remove the global string entirely and just make sure that everything you need is provided inside the parameter and if you need to return a string just return the string that you are returning do your best to make sure that the function is the only one that has to process it because if it has to rely on external components this can lead to a lot of mass in your code so now thanks to this we can type in string equals function and if we run this we're going to get hello and we can also write string equals function again then we're going to get what but this was all handled externally which means we can now use this function in literally any module and it will still work the same way without having to rely on that Global variable which made it really difficult to keep track on where this string was coming from and probably this wasn't the best example because you could just return the new name of course and it would work exactly the same way but the idea Remains the Same try to avoid Global values in your function as much as possible but anyways those were the six tips I wanted to share with you today do let me know in the comment section down below what you think about them whether you have some tips of your own that you would like to share and before I conclude this video I would really like to invite you to our Discord server it's about python there's a lot of cool python developers in there if you have questions I just want to talk about python join the Discord server I'm there I love to chat in there the link is in the description box down below so I'm looking forward to seeing you there but with that being said thanks for watching and I'll see you in the next video
Info
Channel: Indently
Views: 73,799
Rating: undefined out of 5
Keywords: pyton, pyhton, pythn
Id: qvSjZ6AKfXQ
Channel Id: undefined
Length: 9min 54sec (594 seconds)
Published: Wed Dec 14 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.