YTread Logo
YTread Logo

5 Good Python Habits

Jun 04, 2024
This video was brought to you by IND dentle IO learning Python in a simple way, how is it going everyone? In today's video we will see five

good

habits

that you can develop in Python for the first time. I'm going to use a module that I created and this module allows us to connect to the Internet. Now suppose I want to test this module before using it in my script. Here I'm just going to type Connect and run this module and in the console I can see. which actually worked pretty well so let's go to the main part of this script and import this function from that module so from module import connect now I'm really happy to be able to use it in my script so I'm just going to type Connect and I'll run my main module and that's it, it's actually connecting, but oh no, it's connecting twice, what did I do wrong?
5 good python habits
Well the answer is quite simple, every time you import a module you have to load the entire script, that means it is reading each line and since at the bottom of our module we executed a function it also read that line and executed it as Consequently, so the first

good

habit that we should always have is to always check that the name is equal to Main and insert whatever it is. type of functionality that you want to run there, this will ensure that this code will only run if I run this module directly and in some code editors it will also give you this nice green arrow which makes it easy to run the correct script, so now if I run this inside the module, it will be executed exactly the same way.
5 good python habits

More Interesting Facts About,

5 good python habits...

I was able to test it with no problems and if I went back to main.py I can run this script one more time and this time it will just run. this function once but also if you were pretty attentive you would have noticed that I didn't run main. Pi I actually ran the module again by accident, so it doesn't matter what script is inside. Using IF name equals main can also help ensure that you're running the correct script because because of this, usually what I do every time I switch Windows is tap this green arrow when I want to run a script and that will ensure that we're running the current script, so this time I'm running main.py as you can see in the top right corner and finally It also works as a bit of self-documentation because whoever is using your script will know to run if name equals main because some modules may contain information and functions that were only intended to be imported, but by providing if name equals main you can easily see that this script was actually intended to run at a certain point.
5 good python habits
Now let's move on to the second good habit that I think you should form and this is more of a personal preference and I use this because I see it in many programming languages ​​like Java please don't leave the video because I mentioned Java but I use it because I see it in many languages of programming and in my opinion makes it a little more organized. Let's say you have some functionality in your script, whatever is in this script, we just say hello to people and say goodbye. One thing I love to do in all my scripts is define a main entry point where I can tie all the functions together.
5 good python habits
I need together, so here we have a main function that returns none and then you can try whatever you want, you can say hello to people, you can say goodbye, this is where I like to put everything together and then I like to use it in combination with if. The name is equal to Main and then I just run main inside it, it's a very clean way to organize your code. I mean, obviously, you can paste this in here if that's what you want to do and it works too, but having a main entry. point just feels more readable and also simulates what you would do in other programming languages ​​like Java and I know this may seem like a lot of boilerplate code but in many code editors you can define live templates or at least in pycharm you can define one live template that can make this process super easy, for example, if I wanted to rewrite all of that, all I have to do is type Main and that would enter both, then I can enter the functionality that I actually want to run. like say hello and buy and for those of you who used to work in Java, this is very similar to writing psvm to create that public static empty main function that we all loved moving on to tip number three here I have a function called enter club and takes a name, an age and whether the person has ID or not because we want to check or verify that the person is the correct age and has a valid ID before entering the club and also since Bob is a troublemaker I decided to add Bob to the blacklist so if we run into Bob we will tell Bob to leave so I have included functionality to prevent Bob from getting in and also functionality to make sure people have a valid ID and are 21 or more, so I created a bunch of test cases to make sure this actually works, so if we run this you will see that some people can't enter the club, like Bob, get out of here, but James, who is 29, can enter . the club and the other two who are underage and don't have ID can't enter the club so the feature works pretty well but I would have to give it a 1 out of 10 in terms of reusability this only works one way very case specific in case we want to get into the club and this can often be considered bad practice because it handles too many functions in a single function, so instead of defining all the functions here, we are going to create separate functions for each one. one of these, so for now I'm just going to delete both of them and add an ellipsis because I just want to have a placeholder there while I create the other functions and I'm just going to copy and paste the other functions and explain them quickly so that the first one checks that the person is of legal age and the only thing I am doing here is verifying that they have a valid ID and that the age is greater than or equal to 21 years, of course you can take this one step further and create a completely new function to verify how valid an ID is, but this will be enough for this example, we are just going to consider anyone who has a valid ID and who is greater than or equal to 21 years old as an adult, the second checks if the name is equal to Bob and If it's Bob, of course we want to remove Bob from the club or prevent Bob from getting in, so why would this be considered a better approach than what we did before?
Thanks to this approach, we can use this functionality. anywhere in our script in case we want to use it somewhere else later, we don't have to search through our huge function to find this functionality, we created it in one place and it's Ultra reusable now we can check if someone is Bob anywhere part of our script and although the enter club might not change that much, the reuse of our code has increased a lot, so now we can check if it's Bob and handle it accordingly and we can also check if it's an adult and handle it accordingly and if we were to return to run this script, it would work exactly the same way, so for this good habit, all I was trying to say is keep your functions as simple and reusable as possible, never complicate your functions with too much code and always try to separate those concerns to While you can make your code as reusable as possible, you may be unlikely to check if someone is Bob more than once, but you never know and if you ever want to change this logic, you know exactly where to go to do it and what to do. same.
The same goes for this function. Next, we have a fourth good habit that you should use in Python or actually modern Python because it will save you a lot of trouble in the future and I'm talking about type annotations, so let's assume. you are creating a number and this is of type integer, which is quite simple. A lot of people think okay, if I just write the number is equal to 10, it should be pretty obvious that it's an integer with a value of 10 and sure that's pretty obvious, but whoever is working on your code might, by some dumbass, reason, inserting 10 as a float, is the same value, but as you probably know, float is not supported everywhere an integer is supported, so if we were to type an integer here, py charm or our code editor would give us will give a syntax highlight telling us not to do that because it could break the program and in some extreme cases your developers or your colleagues might even enter 10 as a string, providing this type of annotation warns us well in advance.
It's time we are doing something completely ridiculous and maybe for basic types it may be pretty obvious in most cases, but what happens when you start getting more complex types? Then it becomes easier and easier to insert the wrong type and of course if you insert the wrong type your program will not work as expected, but let's look at a different example and in this example we are going to have a function that capitalizes each element and this function does not have any type annotation and again this is all a simple function but it has a lot of bugs as we know we can insert elements but what type of elements can we insert well?
If we were to insert integers, for example, this function will throw an exception because you can't add integers other than a meaningful operation, but we also won't get any warning from the code editor, so we'll just have to run it and get that exception. before changing it to something that actually makes sense, we have no documentation here and of course, for those of you who really like to waste time writing obvious documentation, you can add a docking string that explains exactly what should make the user or they can be explicit from the beginning and they can say type string type list items and that. this returns a list of type strings thanks to these type annotations we are self-documenting this function we are telling the developer or the user exactly what they have to enter and what is going to come out that they are not going to have to experiment with numbers they are not going to Having to read documentation, just take a quick look at the function signature to understand what it does.
I'm not saying that documentation is a bad thing, I'm just saying that documentation is redundant. is a bad thing and also the added benefits of using type annotations is that we get more context, so thanks to adding a list of type strings here, pycharm was able to infer that this was a type string element, which means that will give us the context actions. for strings, if we didn't have this type of annotation, pycharm would have no idea what that is, I mean of course it can be an integer, it can be a float, it can be a boolean, pycharm has no information about what elements are , so it's just a simple advantage of using type annotations is that we get better context actions or more appropriate context actions and again, if we ever try to pass the wrong data types, pycharm will complain here.
I have two variables that do exactly the same thing but with one of them I decided to annotate it as a list of type integer, but here we explicitly told pycharm that we are returning a list of type string without this type annotation, we would not get warnings and then we would run that script before we realize that we are not recovering this list of integers and the silly part is that we can also add 1 2 3 and we still won't get any warnings and a lot of stupid people will say, well, it's pretty obvious that you can't get over a well. integer in programming.
It's never obvious enough There will always be someone who misunderstands something in the last four years of teaching. I've seen the most ridiculous code in the universe. Never underestimate what people think. never think that your code will ever be too obvious because people are always going to misinterpret what they read, whether it's because of the language barrier, whether it's because they're tired, whether it's because you wrote your documentation very badly, it will never be as obvious as it is. You see, so the best thing we can do as developers is to be very obvious with everything we write, although sometimes the code editors are not going to catch everything, for example, we could have this sample list of type integer and obviously , this not only contains integers, it also contains strings, but why not?
I get some warning and that really depends personally on the capabilities of your code editor. I have no idea why py Cham can't recognize that we have the wrong data type within this list, so if you really want to get serious about type annotations, you're going to have to install a static type checker like mypie, so pip installs mypie, but once you have installed mypie, the mypie extension in py Chom will take effect, so it will highlight what I did wrong, for example A and B should not be part of it. from this list of integers my Pi can recognize this but py charm can't but in other cases you might even want to run my Pi directly so that my pi main.py and this will scan ourscript and will check for any errors, and it's usually quite verbose, so it will say that item zero in the list has an incompatible string type, but we were expecting an integer and the same for item number two in the list, so so type annotations are completely optional and your code will run the same with or without them, even if you insert the wrong type annotation your code will still run, but because of type annotations my code rarely fails because I know exactly what I'm doing, I know exactly what type I'm inserting and if I do something silly I'll get a warning that allows me to fix it before I even run the script, so in other words I don't make these silly accidents thanks to typographic annotations and the last good habit I want to share with you today is list comprehensions.
I absolutely recommend that you start using them. Please list the comprehensions if you haven't already because not only are they faster but they can also be easier to read in certain scenarios anyway. To demonstrate this I am going to create a list of people that only contains a few names like James Charlotte Stephanie Mario and Sandra and what I want to do here is retrieve all the names or all the people that have longer names. more than seven characters and to do that I need to create a new list and then I need to check for each person in these people if the length of that person is more than seven then we can add that person to the new list and obviously if I want to see the result of this operation, I'll have to print it to the console, so now if we run this, we should get Charlotte and Stephanie back because those were the only two names that were longer than seven characters, but perform this same operation using a list comprehension, so here I'm going to write long names and that will shadow the name above, but who cares now and in fact apparently my cake cares a lot so I'm just going to comment on this part.
Anyway, we have this variable here and we're going to create the same list using a list comprehension, so here I'm going to write p for p and that will only represent person in people if the length is greater than seven. and it actually has to be the length of p and that took no time to create and it also saved us three lines of code which is epic and if we ran it we would get the exact same result and although I wouldn't recommend this because it starts to look very messy, you could even enter it directly into your printed statement and it would work exactly the same way, although for the moment there is a trade-off and that is that this may be considered less readable because right now we have P for p and people, if the length of p is greater than seven, it would take any developer a moment to understand what that actually means, but fortunately we also have a very descriptive variable name that should give you an idea of ​​the length for each person. it must be more than seven to be considered a long name so in this scenario I would absolutely go with this version of list creation and if you want to be even more specific you can rename this variable to names and then you can simply write for name name name and this could even be considered more readable than what I had before because now we know exactly that these are names and that the length of the name must be more than seven, in some ways it provides better documentation for understanding this list . but anyway, that's all I wanted to cover in today's video.
Let me know if you have any good

habits

of your own or otherwise, as always, thanks for watching and I'll see you in the next video.

If you have any copyright issue, please Contact