Best-practices in using Python Classes in Programming

Foreword

Python is an object oriented programming language and almost everything in Python is an object. Python Classes are object constructors and widely used in Python-programming world. In this article, I am going to present you some best-practices in using Python Classes in programming. In particular, you will get to know:

  1. Decreasing the redundancy of your Classes with “dataclasses” module
  2. Using @property instead of instance variables in Classes

A case where using a Class is better than using dictionaries in Python

Sometimes you may feel that creating Classes is time-consuming as well as complex and therefore, you opt for just creating individual methods and using dictionaries as follows:

from datetime import date
import json
def get_fullname(user):
fullname = f'{user['name']} {user['surname']}'
return fullname
def get_user_age(user):
date_born = user['birthday']
age = date.today().year - date_born.year
return age
def load_user():
with open('./user.json', encoding='utf-8') as file:
return json.load(file)
from datetime import date
import json
from dataclasses import dataclass
@dataclass
class User:
name: str
surname: str
birthday: date
@property
def get_fullname(self):
fullname = f'{self.name} {self.surname}'
return fullname
@property
def get_user_age(self):
date_born = self.birthday
age = date.today().year - date_born.year
return age
def load_user():
with open('./user.json', encoding='utf-8') as file:
return User(**json.load(file))

Using “dataclasses”

Sometimes it is overwhelming to use Class because of the need to define many instance variables as follows:

class User:
def __init__(self, name, surname, birthday, citizenship, address, workplace, job):
self.name = name
self.surname = surname
self.birthday = birthday
self.citizenship = citizenship
self.address = address
self.workplace = workplace
self.job = job
from dataclasses import dataclass
from datetime import datetime
@dataclass
class User:
name: str
surname: surname
birthday: birthday
citizenship: citizenship
address: address
workplace: workplace
job: job
@property
def get_fullname(self):
fullname = f'{self.name} {self.surname}'
return fullname

Using @property instead of instance variables

While creating Classes, sometimes, you define instance variables just to use in the methods as below:

form datetime import date@dataclass
class User:
name: str
surname: surname
birthday: date
age: None
def calc_user_age(self):
date_born = self.birthday
self.age = date.today().year - date_born.year
def age_display(self):
return f"I am {self.age} years old."
form datetime import date@dataclass
class User:
name: str
surname: surname
birthday: date
@property
def age(self):
date_born = self.birthday
age = date.today().year - date_born.year
return age
def age_display(self):
return f"I am {self.age} years old."

Data Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store