Best Practices for Becoming A Good Python Developer

Gaurav Sharma Last Updated : 30 Jun, 2023
7 min read

This article was published as a part of the Data Science Blogathon.

Introduction

Many developers or students write code, a lot of code, but sometimes forget to follow some good practices which can make the code written more efficient and readable. There are some good practices to be followed while programming. It is not just only for Python developers, it is applicable for each programming language like C++, Java, C#, etc. So, in this article, we are going to look at some of those ignored but interesting and important practices to be kept in mind while programming.

Good Python Developer 1

Source: Google Images

Look at the image above. I know it’s quite funny. But the point I want to make here is that Python is this cool. Python programmers can effortlessly do a task whereas others have to sweat a lot for the same task. Hence for python developers, it is important to follow some practices which will make their work even more productive.

So, Let’s get started.

List of Python Best Practises followed by a Good Developer

  • Proper Documentation and Commenting
  • Avoid Creating Global Variables
  • Exception handling
  • Create Separate Environments
  • Use inbuilt functions
  • Structuring of Projects
  • Reviews

1. Proper Documentation and Commenting

This is the first and foremost point I want to put to be a good python developer. It is really important to follow this. Whatever code you are writing should be well documented and commented on by you wherever required. But do you know why we need Code documentation? The answer is very simple. A company project may go for years and years. New developers join the project any time, so now they need to know and understand what is in the code, for this purpose code documentation is important. They can simply refer to it and will get a clear understanding of the code. Just think about that developer if there was no documentation and you simply throw him into the pool of code. It’s really difficult for him without this. Commenting is also for the same purpose, you should be adding comments in your code wherever required.

Jazzy takes care of all your software documentation needs | eMan Good Python Developer

Source: Google Images

There are mainly three types of comments:

a. Single Line comment: They start with the hashtag symbol(#) and last till the end of the line.

Example:

#This is a comment
#Printing Hello World!
print("Hello World!")

b. Multi-Line comment: It is a text enclosed in a delimiter (""") on each end of the comment. There should be no white spaces between delimiter ("""). They are used when the comment text more than one line.

Example:

"""
This is a multi line comment,
spanning over three lines.
This is how it's done.
"""
print("Sum of two numbers:", 45+23)

c. Docstring Comments: It is an in-built feature of
Python. It is used to associate documentation that has been written with Python modules, functions, classes, and methods. It is used just
below the functions, modules, classes to tell what they are doing. The docstring comment is made available by the __doc__ attribute.

Example:

def add(a, b):
    return a+b
print(add.__doc__)

Output:

Add the two numbers a and b

So, use any of these comments and help other developers to know what you have done.

2. Avoid Creating Global Variables

Global variables are those which remain in the application till the last breath i.e., till the time application is running they exist in the code space and can be invoked anytime from anywhere. These are useful at times because of their accessibility but at the same time, this proves to be a disaster for developers. Because of these variables memory also can’t be used efficiently, because a large chunk of your memory will be permanently going to these global variables.

There is one more problem with them. Since every function in your application can access global variables so, it becomes very difficult to identify which function is reading or writing the value of that global variables. Now, to identify this you need to take a closer look at all functions individually which is a headache.

Hence, just avoid these global variables.

3. Exception handling

Exception Handling in Python. Python Exception Handling in 5 Minutes… | by Mayank Gupta | TechnoFunnel | Medium

Source: Google Images

This is a very important practice to be followed by Developers, not only python developers but by all developers of each and every language. So, let me tell you the reasons why it is needed.  Suppose you wrote a program to open a text file and perform certain operations on it like reading the file, closing it, finding the length of the file. This would be a simple code to write but what if some errors like these occurred while execution:

  • What if the file can’t be opened?
  • What if the length of the file can’t be determined?
  • What if the read fails?
  • What if the file can’t be closed?

What if the program threw these errors? Your code will come to a stop and your application will not work. So, these are potential errors that can occur because of any reason. Hence it’s important to handle these exceptions instead of ignoring them. Hence it’s a must to use Exception handling in your code.

Example:

try:
  print(y)
except:
  print("An exception occurred in your code!")

The try block will generate an exception, because y is not defined.

4. Create Separate Environments

Python programmers often try to work in a single environment, they don’t create a separate environment for different projects. It is strictly not good practice to implement all projects in a single environment because you may face some issues in the later stages of development, then you have to change some installations, modify their versions, install new ones. This may affect previous projects which might not accept these new changes as they were dependent on these dependencies. Hence, always prefer to use a new environment.

Fix: Change Python Virtualenv Base Prefix - Vincent Tech Blog Good Python Developer

Source: Google Images

Installing virtualenv

$ pip install virtualenv

Test your installation:

$ virtualenv --version

Using virtualenv

$ virtualenv my_name

Now once you created your virtual environment, you need to activate it. So, for that use this command:

$ source virtualenv_name/bin/activate

When your work is complete in that environment, then close it using this command:

(virtualenv_name)$ deactivate

5.  Use inbuilt functions

Use inbuilt functions of python instead of writing them from scratch, because they are already compiled, optimized for efficient execution and if you are writing them from scratch you are missing out on a big advantage.

6. Structuring of projects

The structuring of projects is also a very important part of development because it shows how efficiently you are going to write your code, how the entire project is coupled. So, if your project has a front end, back end, database part, APIs, and other things and if you have not managed all this then it’s going to be a very messy project. Suppose, later you want to make some changes in your code but you won’t be able to do so, because there is no structure in your code and now it’s a real mess to find what thing is where.

6. Structuring of projects

Source: Google Images

So, for structuring your project there is an interesting tool called cookiecutter. It helps you to create the whole project structure, so try to use this. It is a command-line utility tool that creates projects from cookiecutters (project
templates). For example,  creating a Python package project from a Python package project
template.

GitHub Link : https://github.com/cookiecutter/cookiecutter

Documentation : https://cookiecutter.readthedocs.io

7. Reviews

I hope you must have read this quote:

Alone we can do so little; together we can do so much.

This is applicable in the world of programming also. Try to give reviews on other’s code, and also be open to reviews feedback from others. It’s a good way to learn and grow, learning speed just increases magically. So, try to do this.

Amazon fake reviews: a problem and a 5-star industry - Red Points

Source: Google Images

Frequently Asked Questions

Q1. How can I make my Python code better?

A. To improve your Python code in simple terms:
1. Keep it neat and organized: Use consistent naming, indent your code properly, and add clear comments.
2. Make it reusable: Break your code into smaller parts that can be used again in different places.
3. Make it faster: Identify any slow parts and find ways to make them run more quickly.
4. Handle errors nicely: Prepare your code to handle unexpected situations and show helpful error messages.
5. Explain your code: Add comments that explain what your code does so others can understand it easily.
6. Use helpful tools: Take advantage of ready-made code that others have shared to make your job easier.
7. Test it out: Create tests that make sure your code works correctly, catching any problems before they cause issues.

Q2. Does Netflix use Python?

A. Yes, Netflix uses Python in its systems. Python helps them with different tasks like analyzing data, managing their infrastructure, and creating important systems. Python is popular because it is easy to use and has many useful tools that make it a good choice for Netflix to build and maintain their platform.

End Notes:

These are some very good practices to be followed by every student or developer. They will for sure help you in your programming career. I hope you find the pointers in this article helpful. Let’s connect on Linkedin.

Thanks for reading if you reached here :).

Happy coding!

The media shown in this article are not owned by Analytics Vidhya and are used at the Author’s discretion

Love Programming, Blog writing and Poetry

Responses From Readers

Clear

Python Course
Python Course

Great blog though and great content!

Sowjanya Siddanoor
Sowjanya Siddanoor

This a very useful and insightful article. The article gives an overview about all the best practices and very helpful in improvisation. Thank you for sharing!! Keep posing such informative blogs.

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details