What are the best python practices?

Python is a very popular language in the current world. Everyone is using python for different purposes like web development, machine learning, data science, desktop applications.
And it is very important to follow the best practices in python so code will be readable and understandable for next-generation people.
Let’s know some of the best practices in python
Your code should be well documented so whoever the next person will work on your project can easily understand the code without going deep into it. Like every method, you write should start with method doc. I have shown the Example method doc.
def create_account(account_id, user_id, **kwargs):   """
   This method is used to create the account
   :param account_id:
   :param user_id:
   :param kwargs:
   :return: This method return the account created
   """
   pass
Your python project should have the proper requirement.txt file so it’s easy to clone the project and set up the project through the virtual environment. The requirement should be like following.
alembic==1.0.11
Click==7.0
Flask==1.0.3
Flask-Cors==3.0.8
Your project should be passed to PEP 8 standards. For this, we need the Pylint library. Pylint is very famous for checking the syntax and PEP 8 standards. If you use the JetBrains Team Blog’s Pycharm 2019 it having very good features like Pylint checks.
To install the Pylint follow below step
pip install pylint
Then we just need to pass our file to Pylint then it will tell its analysis
pylint account.py
Use Inline if-else statement instead of a multiline statement like below.
counter = 0
return True if counter else False
When we want an element from the dictionary use Get method instead of the iterative method like below.
temp_dict = {"likes": 3000, "comments": 66}
likes = temp_dict.get("likes",100)
Every API should have the API doc so we that we don’t need an extra effort to create the API docs. Use APIDocGen to create the API doc from the docstring. follow the documentation below:
http://apidocjs.com/
That’s all. Thanks for reading the blog. Please follow and share the blog

No comments:

Powered by Blogger.