🔥Integrate Deep Research into AI Apps
FireCrawl has released the /deep-research
endpoint that enables AI-powered deep research and analysis on any topic.
Simply provide a research query, and Firecrawl will autonomously explore the web, gather relevant information, and synthesize findings into comprehensive insights.
Integrate Deep Research into your AI apps here →
Thanks to Firecrawl for partnering today!
20 Most Common Magic Methods
Here’s a visual that lists the 20 most common magic methods used in Python OOP:
In my experience, these are possibly the only 20 magic methods you would ever need in most Python projects utilizing OOP.
Syntactically, they are prefixed and suffixed with double underscores, such as __len__,
__str__
, and many more. That is why they are also called “Dunder methods” — short for Double UNDERscore.
Here’s a brief description of each of them.
__new__
:This method is invoked before
__init__
to allocate memory to an object.In most cases, we wouldn’t need it.
Yet, at times, I have used this to define checks and allocate memory only when certain conditions are met.
Another common usage is to define singleton classes — classes with only one object.
__init__
:Most common in this list.
This is invoked after memory allocation to assign value to an object’s attributes.
__str__
:Executing
print(obj)
outputs the memory address of the object, which is not interpretable.Defining this method lets us print the object in a readable format.
__int__
: Invoked when we execute →int(obj)
.__len__
: Invoked when we execute →len(obj)
.__call__
:Invoked when a class object is called as a function →
obj()
.
__getitem__
: Invoked when an object is indexed →obj[key]
.__setitem__
: Invoked when an object is indexed and a value is set →obj[key]=value
.__delitem__
: Invoked when an object’s index is deleted →del obj[key]
.__contains__
: Invoked when thein
operator is used →item in obj
.__bool__
: Invoked when an object is used in a boolean context →if obj or bool(obj)
.__iter__
: Invoked when we iterate over an object →for x in obj
.__eq__
: Invoked when==
operator is used to compare two objects →obj1 == obj2
.__ne__
: Invoked when!=
operator is used to compare two objects →obj1 != obj2
.__gt__
:Invoked when
>
operator is used to compare two objects →obj1 > obj2
.Other than
__gt__
, we also have:__lt__
: less than.__le__
: less than or equal to.__ge__
: greater than or equal to.
__add__
: Invoked when two objects are added →obj1 + obj2
.__mul__
: Invoked when two objects are multiplied →obj1 * obj2
.__abs__
: Invoked when we compute the absolute value of an object:abs(obj)
.__neg__
: Invoked when the unary operator-
(minus) is used on an object →-obj
.__invert__
: Invoked when~
(tilde) operator is used to invert an object →~obj
.
That’s it.
Now you know the most common Magic methods in Python and how they are used.
We went into much more detail and covered many advanced concepts and programming details of Python OOP here: Object-Oriented Programming with Python for Data Scientists.
Also, if you want to get really good at Python OOP, learn about Python Descriptors.
I find them to be massively helpful in reducing work and code redundancy while also making the entire implementation much more elegant.
We covered it in this newsletter here: Define Elegant and Concise Python Classes with Descriptors.
👉 Over to you: Did I miss any common magic methods? If yes, which one(s)?
Thanks for reading!
P.S. For those wanting to develop “Industry ML” expertise:
At the end of the day, all businesses care about impact. That’s it!
Can you reduce costs?
Drive revenue?
Can you scale ML models?
Predict trends before they happen?
We have discussed several other topics (with implementations) that align with such topics.
Here are some of them:
Learn sophisticated graph architectures and how to train them on graph data: A Crash Course on Graph Neural Networks – Part 1.
So many real-world NLP systems rely on pairwise context scoring. Learn scalable approaches here: Bi-encoders and Cross-encoders for Sentence Pair Similarity Scoring – Part 1.
Learn techniques to run large models on small devices: Quantization: Optimize ML Models to Run Them on Tiny Hardware.
Learn how to generate prediction intervals or sets with strong statistical guarantees for increasing trust: Conformal Predictions: Build Confidence in Your ML Model’s Predictions.
Learn how to identify causal relationships and answer business questions: A Crash Course on Causality – Part 1
Learn how to scale ML model training: A Practical Guide to Scaling ML Model Training.
Learn techniques to reliably roll out new models in production: 5 Must-Know Ways to Test ML Models in Production (Implementation Included)
Learn how to build privacy-first ML systems: Federated Learning: A Critical Step Towards Privacy-Preserving Machine Learning.
Learn how to compress ML models and reduce costs: Model Compression: A Critical Step Towards Efficient Machine Learning.
All these resources will help you cultivate key skills that businesses and companies care about the most.