What is a class in OOPs in python how to create it and use in program

This recipe explains what is a class in OOPs in python how to create it and use in program

Recipe Objective

This recipe explains what is a class and how to create it and use it in a program.

Access Wine Quality Prediction using Machine Learning Code

Step 1: Defining a Class in Python

We define a class by using the class keyword. A new class is creating a new local namespace corresponding to the class where all of its attributes are defined, these attributes are either function or data. Some special attribute begins with double underscores __. As soon as we create a class, a new class object is created with the same name which allows us to initiate other objects of the class and to access different attributes.

 

    class kid:

    # class attribute
    student = "Student"

    # instance attribute
    def __init__(self, name, age):
        self.name = name
        self.age = age

Step 2: Creating an Object in Python

We can access the attributes of objects using the object name prefix these attributes are either method or data. Methods are nothing but corresponding functions of that class. If we call a method with n arguments it is the same as calling the corresponding function with an argument list that is created by inserting the method's object before the first argument therefore the first argument in a class must be that object.

 

    # instantiate the kid class
    Sam = kid("Sam", 13)
    Max = kid("Max", 14)

    # access the class attributes
    print("Sam is a {}".format(Sam.__class__.student))
    print("Max is also a {}".format(Max.__class__.student))

    # access the instance attributes
    print("{} is {} years old".format( Sam.name, Sam.age))
    print("{} is {} years old".format( Max.name, Max.age))

 

What Users are saying..

profile image

Gautam Vermani

Data Consultant at Confidential
linkedin profile url

Having worked in the field of Data Science, I wanted to explore how I can implement projects in other domains, So I thought of connecting with ProjectPro. A project that helped me absorb this topic... Read More

Relevant Projects

A/B Testing Approach for Comparing Performance of ML Models
The objective of this project is to compare the performance of BERT and DistilBERT models for building an efficient Question and Answering system. Using A/B testing approach, we explore the effectiveness and efficiency of both models and determine which one is better suited for Q&A tasks.

NLP Project for Beginners on Text Processing and Classification
This Project Explains the Basic Text Preprocessing and How to Build a Classification Model in Python

AWS MLOps Project to Deploy a Classification Model [Banking]
In this AWS MLOps project, you will learn how to deploy a classification model using Flask on AWS.

Image Segmentation using Mask R-CNN with Tensorflow
In this Deep Learning Project on Image Segmentation Python, you will learn how to implement the Mask R-CNN model for early fire detection.

Recommender System Machine Learning Project for Beginners-4
Collaborative Filtering Recommender System Project - Comparison of different model based and memory based methods to build recommendation system using collaborative filtering.

Learn to Build an End-to-End Machine Learning Pipeline - Part 1
In this Machine Learning Project, you will learn how to build an end-to-end machine learning pipeline for predicting truck delays, addressing a major challenge in the logistics industry.

Build OCR from Scratch Python using YOLO and Tesseract
In this deep learning project, you will learn how to build your custom OCR (optical character recognition) from scratch by using Google Tesseract and YOLO to read the text from any images.

Create Your First Chatbot with RASA NLU Model and Python
Learn the basic aspects of chatbot development and open source conversational AI RASA to create a simple AI powered chatbot on your own.

AWS Project to Build and Deploy LSTM Model with Sagemaker
In this AWS Sagemaker Project, you will learn to build a LSTM model on Sagemaker for sales forecasting while analyzing the impact of weather conditions on Sales.

PyTorch Project to Build a GAN Model on MNIST Dataset
In this deep learning project, you will learn how to build a GAN Model on MNIST Dataset for generating new images of handwritten digits.