Advance Python Series-Magic Methods In Classes

Krish Naik · Beginner ·🧬 Deep Learning ·5y ago

Key Takeaways

Introduction to magic methods in Python classes using Kite plugin

Full Transcript

hello all my name is krishnak and welcome to my youtube channel so guys we will be continuing the advanced python series and in this particular video we are going to discuss about magic methods and classes now why it is called magic because as you know there is no such thing as magic right something some trick is actually there in the back end but still we say yes you can do some kind of magics i think you have seen so many magicians who will be taking out vision will be taking out some or the other thing from their hands right so similarly over here why we call this as a magic methods because something is happening in a tricky way okay so let me just show you if you're new to this video please do subscribe the channel press the bell notification icon and let's begin this particular session to begin with guys first of all what we are going to do is that i have created a car class and in this car class we obviously have a initialization constructor and then we have created four variables that this particular car object may be taking so windows doors and engine type and then i'm initializing all these particular variables and then after that i also have a class method which is a public method also and this all variables are also public with respect to access modifiers and this function is basically saying that the person drives the car so drive is a functionality of a car and this is basically a car blueprint now what i can do is that if i write and this is i think you've seen this particular code a lot okay suppose if i write c is equal to car so i'm just initializing the object and this i'm just going to give the values like 4 comma 5 comma engine type that i'm going to give is diesel okay so once i execute this here you will be able to see that first of all let me just execute this once again okay and then if i create this particular object i can see this object see that has been created initially you are getting that specific message i'll tell you why we had got that specific message but just before recording this video and made it some changes over here now here you can see that it basically specifies that this is an object of this particular car at this specific memory location right remember one thing guys this is your initialization constructor this is your method that is present within the class now if i write dir of c right so here you can see all these attributes that are present this all that you can see underscore underscore class underscore underscore underscore underscore dick underscore underscore so all these attributes all these methods that you actually see are actually called as magic method in class why because just understand we are trying to create a object here right we are trying to create an object here and how it is actually happening internally internally through some magic right this underscore underscore init is basically getting called okay again i'm telling you guys in if you really want to create any objects you basically create an object of a class by using that particular class and providing these particular values that are present in it right and this underscore underscore and it underscore underscore is called from the back end right as a times of magic and then you are actually creating an object so underscore underscore init is basically present over here and this is called as the magic method now let me just say show you some more examples suppose i have this particular method that is underscore underscore str underscore underscore and remember guys we can override this magic methods too we can override it so let us take one example where i'm using underscore underscore str underscore underscore now see guys if i just print c by default i am getting the specific message let me just write print c okay if i am printing see it is saying that okay it is a car object at this specific memory location the reason it is being able to print this message because by default by default there is a magic method whenever we write print a class object and if i go and see direct of c it is in short calling this underscore underscore str underscore underscore method this is the magic method being called and by default this function or this method displays a message saying that this is an object with respect to some memory location now if i really want to override it what do i do i will just write something like this see definition underscore underscore str underscore underscore and here inside that i'm will be taking a parameter self and here now i can override my functionality i can say that okay this in just a second so i will say print the object has been initialized something like this i'm just printing some generic message let me just write this in quotes okay just to show you that yes we can overwrite this magic method we can make it display something else now once i execute this now this in short says that you underscore underscore str underscore underscore is being overridden and what is the functionality of this whenever you are trying to print a class object by default whatever is present over here that is going to get printed if you have over read it by default it shows you this car object now see this again i'll try to execute this and again i'll try to say print c okay now it is giving me an error saying that underscore underscore str return non-string now see guys this string is getting printed but we cannot print it inside it so we have to return it because this method this magic method returns something okay it may be a string it may be something right so i'll just execute it once again and just change from print to return now let me just print this particular c now when i print the c the previous functionality will be overridden by this and remember guys you will be only able to see when you are using this print functionality see this once i execute it here you will be able to see this one if i try to just print or if i just try to display c it will tell you that okay it is a object of a car remember the print statement is getting overridden because we have overridden this particular method which is this magic method so here if i go and write print see so here you will be able to see that object has been initialized so this is the message that is getting displayed now similarly i may have many methods let's take one more magic method okay so magic method i can take something like this let's see so there is something called a size off okay now if i really want to see what is the size of this particular object i may use something like this c dot underscore underscore size off and if i just try to print this method you will be able to see that i'll be getting 32 this basically says that this entire object is consisting of 32 bytes now if i want to overwrite this all i have to do is that i have to basically override this functionality saying there's diff underscore underscore size of underscore underscore and here i'm going to use sorry i am going to use self okay self and here i'm just going to say return the overall size is the overall size is i'll just use a placeholder and use some advanced formatting saying that dot format and here i can say that whatever the size is right the size is probably uh this this size i can actually print it somewhere or just let me just write as generic message this displays the size size of the object something like this i'm just displaying some generic message guys so that you don't have confusion in understanding it so once i execute it i execute this print c will obviously tell me the object has been initialized now if i try to execute this here you'll be able to see that this displays the size of the object this is how we override now why it is called as a magic method because we are just calling this right we are just calling this functionality internally something is happening like suppose if in this particular case i'm creating an object internally this init method is called if i am printing this internally this is basically getting called like that i can have a lot of functionalities let me write one more functionality guys there is something called as underscore underscore new let's see where it is or this method now this underscore underscore new is basically called before init if you don't believe me just see over here i'll just write definition of underscore underscore new underscore underscore and here i'm just going to use all these four parameters whichever i'm actually calling and i'll say that print the object is getting initialized something like this the object has started getting initialized something like this guys now whenever we create an object first this method will be called and then init method will be called if you don't trust me see over here guys i'll execute it and here you can see that the object has started getting initialized so this has got executed then the initialization constructor gets executed now i hope you are understanding why we call it as a magic method because some magic happens in the back end that is nothing but some trick where it is helping you to create an object in an efficient way so i hope you like this particular video please do subscribe the channel if you haven't already subscribe i'll see you on the next video have a great day thank you and all and yes guys i've started this python playlist the link will be given in the description of this particular video till then have a great day bye bye

Original Description

github: https://github.com/krishnaik06/Machine-Learning-in-90-days/blob/master/Section%201-%20Python%20Crash%20Course/Advance%20Python%20Series-Magic%20Methods%20In%20Classes.ipynb ⭐ Kite is a free AI-powered coding assistant that will help you code faster and smarter. The Kite plugin integrates with all the top editors and IDEs to give you smart completions and documentation while you’re typing. I've been using Kite for a few months and I love it! https://www.kite.com/get-kite/?utm_medium=referral&utm_source=youtube&utm_campaign=krishnaik&utm_content=description-only Starter In Data Science 1 Complete Machine Learning Playlist:(Top 24 videos) https://www.youtube.com/playlist?list=PLZoTAELRMXVPBTrWtJkn3wWQxZkmTXGwe 2 Statistics in Machine Learning:(Understand some Concepts With Respect To Data)- Complete Playlist https://www.youtube.com/playlist?list=PLZoTAELRMXVMhVyr3Ri9IQ-t5QPBtxzJO 3. Feature Engineering(Complete Playlist) https://www.youtube.com/playlist?list=PLZoTAELRMXVPwYGE2PXD3x0bfKnR0cJjN 4. Continue The Complete Machine Learning Playlist(24-all the videos) 5. Live Stream Playlist:(Top 10 videos) https://www.youtube.com/playlist?list=PLZoTAELRMXVPUyxuK8AphGMuIJHTyuWna 6. Machine Learning Pipelines https://www.youtube.com/playlist?list=PLZoTAELRMXVMcRQwR5_J8k9S7cffVFq_U 7. Complete Deep Learning Playlist: Tensorflow And Keras-https://www.youtube.com/playlist?list=PLZoTAELRMXVPGU70ZGsckrMdr0FteeRUi Pytorch: https://www.youtube.com/playlist?list=PLZoTAELRMXVNxYFq_9MuiUdn2YnlFqmMK 8. Live Projects Playlist: https://www.youtube.com/playlist?list=PLZoTAELRMXVOFnfSwkB_uyr4FT-327noK 9. Live stream Playlist: https://www.youtube.com/playlist?list=PLZoTAELRMXVPUyxuK8AphGMuIJHTyuWna 10.Docker Playlist: https://www.youtube.com/playlist?list=PLZoTAELRMXVNKtpy0U_Mx9N26w8n0hIbs 11. Mongodb: https://www.youtube.com/playlist?list=PLZoTAELRMXVN_8zzsevm1bm6G-plsiO1I 12. Machine Learning Interviews: https://www.youtube.com/playlist?list=PLZoTAELRMXVM0zN0cgJrfT6T
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Krish Naik · Krish Naik · 0 of 60

← Previous Next →
1 Natural Language Processing|Stemming
Natural Language Processing|Stemming
Krish Naik
2 Natural Language Processing|BagofWords
Natural Language Processing|BagofWords
Krish Naik
3 Gaussian distribution or Normal Distribution in statisctics
Gaussian distribution or Normal Distribution in statisctics
Krish Naik
4 Natural Language Processing|TF-IDF for Machine Learning| Text Prerocessing
Natural Language Processing|TF-IDF for Machine Learning| Text Prerocessing
Krish Naik
5 Log Normal Distribution in Statistics
Log Normal Distribution in Statistics
Krish Naik
6 Covariance in Statistics
Covariance in Statistics
Krish Naik
7 Confusion matrix, Precision, Recall| Data Science Interview questions
Confusion matrix, Precision, Recall| Data Science Interview questions
Krish Naik
8 Tutorial 44-Balanced vs Imbalanced Dataset and how to handle Imbalanced Dataset
Tutorial 44-Balanced vs Imbalanced Dataset and how to handle Imbalanced Dataset
Krish Naik
9 Implementing a Spam classifier in python| Natural Language Processing
Implementing a Spam classifier in python| Natural Language Processing
Krish Naik
10 Tutorial 11-Exploratory Data Analysis(EDA) of Titanic dataset
Tutorial 11-Exploratory Data Analysis(EDA) of Titanic dataset
Krish Naik
11 Face Recognition using open CV and VGG 16 Transfer Learning
Face Recognition using open CV and VGG 16 Transfer Learning
Krish Naik
12 Pedestrian Detection using OpenCV from Videos
Pedestrian Detection using OpenCV from Videos
Krish Naik
13 Face and Eye Detection from Videos using HAAR Cascade Classifier
Face and Eye Detection from Videos using HAAR Cascade Classifier
Krish Naik
14 Reading, Writing and Displaying images with Opencv| OpenCV Tutorial
Reading, Writing and Displaying images with Opencv| OpenCV Tutorial
Krish Naik
15 OpenCV Installation | OpenCV tutorial
OpenCV Installation | OpenCV tutorial
Krish Naik
16 Face and Eye Detection from Images using HAAR Cascade Classifier
Face and Eye Detection from Images using HAAR Cascade Classifier
Krish Naik
17 Car Detection using HAAR Cascade and Opencv from Videos.
Car Detection using HAAR Cascade and Opencv from Videos.
Krish Naik
18 Using OpenFace for Face recognition in Keras
Using OpenFace for Face recognition in Keras
Krish Naik
19 OpenPose Tutorial with Tensorflow
OpenPose Tutorial with Tensorflow
Krish Naik
20 Multiple Linear Regression using python and sklearn
Multiple Linear Regression using python and sklearn
Krish Naik
21 Dimensional Reduction| Principal Component Analysis
Dimensional Reduction| Principal Component Analysis
Krish Naik
22 Movie Recommender System using Python
Movie Recommender System using Python
Krish Naik
23 TPR,FPR,FNR,TNR, Confusion Matrix
TPR,FPR,FNR,TNR, Confusion Matrix
Krish Naik
24 Precision, Recall and F1-Score
Precision, Recall and F1-Score
Krish Naik
25 Artificial Neural Network for Customer's Exit Prediction from Bank
Artificial Neural Network for Customer's Exit Prediction from Bank
Krish Naik
26 GridSearchCV- Select the best hyperparameter for any Classification Model
GridSearchCV- Select the best hyperparameter for any Classification Model
Krish Naik
27 RandomizedSearchCV- Select the best hyperparameter for any Classification Model
RandomizedSearchCV- Select the best hyperparameter for any Classification Model
Krish Naik
28 K Nearest Neighbor classification with Intuition and practical solution
K Nearest Neighbor classification with Intuition and practical solution
Krish Naik
29 K Means Clustering Intuition
K Means Clustering Intuition
Krish Naik
30 Create custom Alexa Skill- Lambda function- Part2
Create custom Alexa Skill- Lambda function- Part2
Krish Naik
31 Hierarchical Clustering intuition
Hierarchical Clustering intuition
Krish Naik
32 Implement Transfer Learning with a generic Code Template
Implement Transfer Learning with a generic Code Template
Krish Naik
33 Gender Classifier and Age Estimator using Resnet Convolution Neural Network
Gender Classifier and Age Estimator using Resnet Convolution Neural Network
Krish Naik
34 Unlock Your Application With Your Face using OpenCV
Unlock Your Application With Your Face using OpenCV
Krish Naik
35 Draw rectangle from webcam and sketch process it on a live feed
Draw rectangle from webcam and sketch process it on a live feed
Krish Naik
36 Complete Life Cycle of a Data Science Project
Complete Life Cycle of a Data Science Project
Krish Naik
37 How we can apply Machine Learning in Finance
How we can apply Machine Learning in Finance
Krish Naik
38 Deep Learning in Medical Science
Deep Learning in Medical Science
Krish Naik
39 How to switch your career to Data Science.
How to switch your career to Data Science.
Krish Naik
40 Linear Regression Mathematical Intuition
Linear Regression Mathematical Intuition
Krish Naik
41 Handle Categorical features using Python
Handle Categorical features using Python
Krish Naik
42 Machine Learning Algorithm- Which one to choose for your Problem?
Machine Learning Algorithm- Which one to choose for your Problem?
Krish Naik
43 DBSCAN Clustering Easily Explained with Implementation
DBSCAN Clustering Easily Explained with Implementation
Krish Naik
44 Curse of Dimensionality Easily explained| Machine Learning
Curse of Dimensionality Easily explained| Machine Learning
Krish Naik
45 Feature Selection Techniques Easily Explained | Machine Learning
Feature Selection Techniques Easily Explained | Machine Learning
Krish Naik
46 Tutorial 29-R square and Adjusted R square Clearly Explained| Machine Learning
Tutorial 29-R square and Adjusted R square Clearly Explained| Machine Learning
Krish Naik
47 Cross Validation using sklearn and python | Machine Learning
Cross Validation using sklearn and python | Machine Learning
Krish Naik
48 Handling Missing Data Easily Explained| Machine Learning
Handling Missing Data Easily Explained| Machine Learning
Krish Naik
49 Deploy Machine Learning Model using Flask
Deploy Machine Learning Model using Flask
Krish Naik
50 Deployment of Deep Learning Model using Flask
Deployment of Deep Learning Model using Flask
Krish Naik
51 How to Visualize Multiple Linear Regression in python
How to Visualize Multiple Linear Regression in python
Krish Naik
52 K Nearest Neighbour Easily Explained with Implementation
K Nearest Neighbour Easily Explained with Implementation
Krish Naik
53 Predicting Heart Disease using Machine Learning
Predicting Heart Disease using Machine Learning
Krish Naik
54 Predicting Lungs Disease using Deep Learning
Predicting Lungs Disease using Deep Learning
Krish Naik
55 Stock Sentiment Analysis using News Headlines
Stock Sentiment Analysis using News Headlines
Krish Naik
56 Random Forest(Bootstrap Aggregation) Easily Explained
Random Forest(Bootstrap Aggregation) Easily Explained
Krish Naik
57 Voting Classifier(Hard Voting and Soft Voting Classifier)
Voting Classifier(Hard Voting and Soft Voting Classifier)
Krish Naik
58 Credit Card Fraud Detection using Machine Learning from Kaggle
Credit Card Fraud Detection using Machine Learning from Kaggle
Krish Naik
59 Hyperparameter Optimization for Xgboost
Hyperparameter Optimization for Xgboost
Krish Naik
60 Tutorial 45-Handling imbalanced Dataset  using python- Part 1
Tutorial 45-Handling imbalanced Dataset using python- Part 1
Krish Naik

Related Reads

Up next
Image Classification with ml5.js
The Coding Train
Watch →