Typefully

Designing a Flexible Product Class

Avatar

Share

 • 

A year ago

 • 

View on X

Python CLASSMETHOD decorator clearly explained !!!
Imagine you are working as a backend engineer for a company whose software is used by various retail outlets. You've been tasked to create a class 'Product' which helps create a new product by taking in name, price & quantity_in_stock. You do a simple harmless implementation ↓
Your firm now get's a new retail outlet who want to use your software but need to create the product not based on name, price & quantity_in_stock but a product_code Your 'Product' class looks at your like this
'classmethod' decorator to the rescue which helps create alternatie constructors i.e. initialize a class in more ways thann one Modified 'Product' class which takes cls & product_code & creates instance of Product `cls` refers to the Product class & is just a naming convention
Here's how the `Product` class now performs & can create products in more ways. Now, no matter how many retail outlets your sales team manage to onboard you know how to effectively cater to their product creation requirements :)
Hope you liked this dead simple explanation, if you did you can try out this code & leave a ⭐ on my GiTHub repo github.com/jaydeepkarale/100DaysOfPython/tree/main
Avatar

Jaydeep

@_jaydeepkarale

Follow Me If You Like 🐍 Python 🤖 Machine Learning/AI ⚙️ Software Engineering 📊 Data Visualization 📩 DM For Collab youtube.com/@jaydeepkarale