Have you been writing code for some time now but still find that your code is kind of hard to maintain and honestly ugly. Or maybe you are a new pipeline TD and you just come to the realization that your studio implements code reviews and you don’t want to look bad. This topic is going to do seven easy Python Tips for Beginner that if you implement it will level up.
1. Follow PEP 8
Follow the pep eight style guide. The first thing you need to understand is code is read more often than it is written. And this is fundamental it’s both that you need to keep in mind. If you’re just learning Python it’s absolutely great to follow and learn the style guide. You will meet more Python code that adheres to the PEC 8 style guide. Then the ones that don’t so it’s imperative that you familiarize yourself with that and maybe even adopt that as your style guide. Now to help yourself do that use a Linter Pylint is a great Linter. That I highly recommend you can find that as a plug-in in PyCharm you can also find that as a plugin in sublime. So whatever integrated development environment or text editor you use make sure it’s an editor that will allow you to use a Python Minter.
The Python winter is the number one thing that’s going to help you adhere to the style guide. Because it is always pointing out to you areas where you are not adhering to the style guide. And so very quickly it trains your muscles it trains your hand to sit trains you to know what to look for in order to adhere to the PAP eight style guide. So highly recommend a great tip to use Python Tips.
2. Intuitive and Concise Names
Number two intuitive but concise names take the time to name rename your variable names, function names, class names, and module names everything so that they are as intuitive and as easy to understand as possible. your variable and function names should feel like you’re reading plain English. This means variable names like ABC, XYZ those are a big no-no don’t be afraid to refactor and rename as your code changes. As you develop your code you’re going to understand it better and hence you’re going to be able to name your variables and functions better if you’re interested in the details. For the tips that I just shared with you check out the book clean code by Robert C Martin, it’s an exceptionally good book. That I recommend anyone who codes scripts to actually read. so make sure to check it out.
3. Python Property Decorator
Python Tips number three uses the Python property decorator I often see code with a lot of getting and set functions like getting radius set height and all of these come from a traditional. Maybe Java or C background where we use a get or set function in order to change or access values in a property of a class. However, in Python, you can always access the properties of a class simply by using the dot notation by going object dot property. So you could say circle dot radius. If you find that you are going to need to do some sort of processing on your property, use the Python property decorator.
The Python property decorator will allow you to do some sort of processing to your data before you assign it to your properties. Such as may be receiving a string as a path to a file but then creating a path object out of that string and assigning. That path object into your private property you’ll find that if you use the dot notation for Python properties as well as the property decorator your code will be free of gets and sets which makes it so much cleaner and easier to read.
4. Short Function Minimal Arguments
Tip number four keep your function lines short and your arguments minimal. Long functions tend to do more than one thing and are difficult to read. If you find your function lines are too long you probably need to break them down. I recommend less than 10 to 15 lines of code per function. For function arguments, one to two is pretty good three is a stretch anything above three just makes the function really hard to use. If you find that you need to pass more than three arguments into a function it’s probably time to think about refactoring your arguments into a single object or dictionary.
5. Use Comments to Answer Why
Tip number 5, writes comments that explain why but not how it may surprise you. But except for docstrings I hardly write comments. Begin a mistake too many comments explaining what the code is doing it may seem counter-intuitive. But your code should be so simple and well-named that it explains itself.
If you’ve been following the tips above in naming your variable as well in keeping your functions short then you really don’t need to write such comments. Writing comments to explain how the code works violate that do not repeat yourself principle or drive principle. It makes it hard to maintain your documentation and your comments when your code changes and your code is going to change constantly. Starting with the pseudocode that lays down how your code is going to work before writing a single line of code is a great practice. But remove your pseudocode when you are done. I tend to write comments to explain the reason why I am doing something to the user if they don’t have specific domain knowledge. Such as I’m doing ABC in this order because Maya requires me to perform these steps in this exact order. It is the best Python Tips for beginner.
6. Practice EAFP
Tip number 6 practice it’s easier to ask for forgiveness than permission. A classic example where AAFP works really well is when we need to save a file or open a file. A lot of beginner Python programmers adopt ELBYL or look before you leap. So what do we do is they would go ahead and check if the folder exists if the file exists before they would open a file or before they would write to a folder. Now that tends to open your code to a race condition. That means after you’ve checked that the file exists and before your next line in which you try to open the file.
Some processes or some users over the network might have just deleted the file. In which case when you try to open the file it would fail. Even though in your previous line you’ve already checked that the file exists. So now you can see why to look before you leap isn’t necessarily that great. Instead, the Python way is to just go ahead and open the file and if it fails to sit will throw you an error that you can then use a try accept statement to handle. That is a more explicit way to code it eliminates the race condition. There are fewer repetitions and in general, your code is easier to read.
7. Avoid The Universal TRY…EXCEPT…
Tip number seven avoid the universal try to accept. The universal try accept is so bad that the website real Python actually calls it the most diabolical Python anti-pattern. If you just use try accept or try to accept exception what you’re basically doing is hiding errors. Even the ones that you don’t know to expect, you’re sweeping everything under the rug. So when the program fails or a bug is discovered it makes it ten times more difficult for you to find where that error occurs. In fact, you’ve also thrown away the helpful traceback that usually comes with an exception.
Do you have any easy Python Tips and tricks that you use to level up your PI foo share them in the comments below.