4 Python Class Things We Use In Prod (& 3 We Don’t)
4 Python Class Things That We Actually Use in Production Code
1) classmethod & staticmethod
In production code, we try as much as possible not to declare variables that we do not use.
if we need instance attributes, we use instance methods
if we don’t need instance attributes, but need class attributes, we use class methods, decorated with the
@classmethoddecoratorif we need neither instance nor class attributes, we use static methods, decorated with the
@staticmethoddecorator
.get_scientific_nameonly requires class attributes, so it’s considered bad practice to make it an instance method with access toself.speakdoesn’t require class attributes nor instance attributes, so it’s considered bad practice to make it anything other than a static method.
2) lazy loading
Eager loading is the practice of loading everything at the start when our class is initialized. Conversely, lazy loading is the practice of loading data only when another object.
In production code, we often implement lazy loading — calling certain APIs when they are explicitly called for.
If nobody calls
.get_data1(),.call_some_api()will never be called.When
.get_data1()is called for the first time,.call_some_api()is called once, and the retrieved data is cached.When
.get_data1()is called for the second time onwards,.call_some_apiis NOT called. We simply our data from the cached value.
3) Abstract Classes
As an undergraduate, I remember learning about this and going — why do I need to do this, and isn’t this just extra steps?
After writing production code for a while, I realised how useful this is in preventing your prod codebase from becoming spaghetti.
If a child class inherits from BaseChecker:
the child class must override the
runabstract methodwhen overriding the
runmethod, it must returndict[str, set[str]]doing this forces some structure in the code base
4) Mixins
In Python, a class can inherit from multiple classes.
A Mixin is a special class designed to provide specific functionality to classes that inherit from it.
Consider the following classes:
This can be rewritten using Mixins to contain less repeated code:
Here, notice that the RunMixin is designed to inject the run method, while the SwimMixin is degisned to inject the swim method. Classes that want these methods can simply inherit from these respective mixins.
Quick Pause
Consider buying my ebook — 256 Python Things I Wish I Knew Earlier — a distilled collection of hard-earned insights from writing 500,000+ lines of code over 9 years. Save yourself years of trial and error.
Link: https://payhip.com/b/xpRco
Conversely, 3 Python Class Things That We Hardly Use in Production Code
1) @property
What we were taught in Python OOP class in university:
How we write this in production code:
At least in the production Python code that I see, nobody really bothers to use @property at all.
Reason: using @property doesn’t actually provide that much value, and causes extra lines of code instead. Note that “private” attributes such as __name are not truly private, and can be bypassed using ._Dog__name instead.
2) Metaclasses
A metaclass is a class whose instance is another class. Metaclasses can be used to define how classes are created and behave.
For instance, MyMetaclass here adds an extra hello method to classes that use it to initialize.
While metaclasses are pretty cool, we don’t see them that much in production code.
Reason: In my opinion, using metaclasses hurts readability by a tiny bit as there are simpler, more straightforward ways to achieve this behaviour eg. simple inheritance or mixins.
3) Most magic methods
In Python OOP class, we were taught that magic methods unlock many cool and strange behaviours. Some examples:
While magic methods allow us to do lots of cool syntax magic, they aren’t actually used that much in production code, other than the basic ones like __init__, __str__
Reason: we don’t usually need them unless we’re writing some cool library code eg. pathlib which uses / to concatenate paths, pandas etc.
Conclusion
Hopefully this was clear and easy to understand
If You Wish To Support Me As A Writer
Buy my book — 256 Python Things I Wish I Knew Earlier at https://payhip.com/b/xpRco
Clap 50 times for this story
Leave a comment telling me your thoughts
Highlight your favourite part of the story
Thank you — these tiny actions go a long way, and I really appreciate it.
LinkedIn: https://www.linkedin.com/company/the-python-rabbithole











