Skip to main content

Command Palette

Search for a command to run...

Understanding Object-Oriented Programming in Python: Day 35 Guide

A Deeper Look into Python's Object-Oriented Programming: Part 2

Published
2 min read
Understanding Object-Oriented Programming in Python: Day 35 Guide
A

I'm Archana, pursuing Graduation in Information technology and Management. I'm a fresher with expertise in Python programming. I'm excited to apply my skills in AI/ML learning , Python, Java and web development. Looking forward to collaborating and learning from industry experts.

Introduction :

Welcome back to my Python journey! Yesterday, I started the OOPs concept in python in python.

Today, I dove into something more on OOPs concept in python, Let's explore what I learned!

Constructor Concept:

Constructor is a special method in python.

The name of the constructor should be __init__(self)

Constructor will be executed automatically at the time of object creation.

The main purpose of constructor is to declare and initialize instance variables.

Per object constructor will be exeucted only once.

Constructor can take atleast one argument(atleast self) Constructor is optional and if we are not providing any constructor then python will provide default constructor.

Example :

def __init__(self,name,rollno,marks):

self.name=name

self.rollno=rollno

self.marks=marks

Program to demonistrate constructor will execute only once per object


class Test:

def __init__(self):

print("Constructor exeuction...")

def m1(self):

print("Method execution...")

t1=Test()

t2=Test()

t3=Test() t1.m1()

class Student:

''''' This is student class with required data'''

def __init__(self,x,y,z):

self.name=x

self.rollno=y

self.marks=z

def display(self):

print("Student Name:{}\nRollno:{} \nMarks:{}".format(self.name,self.rollno,self.marks))

s1=Student("jithon",101,80)

s1.display()

s2=Student("Sujata",102,100)

s2.display()

Differences between Methods and Constructors:

Method

. Name of method can be any name 1.

. Method will be executed if we call that method

. Per object, method can be called any number of times .

Constructor

. Constructor name should be always __init__ .

. Constructor will be executed automatically at the time of object creation.

. Per object, Constructor will be executed only once .

. Inside method we can write business logic 4. Inside Constructor we have to declare and initialize instance variables

Challenges :

  1. Understanding python databases.

  2. Handling errors.

Resources :

  1. Official Python Documentation: python OOPs concept

  2. W3Schools' Python Tutorial: python OOPs concept

  3. Scaler's Python Courses : python OOPs concept

Goals for Tomorrow :

  1. Explore something more on Types of Variables in python.

Conclusion :

Day 35’s a success!

What are your favorite Python resources? Share in the comments below.

Connect with me :

GitHub: [ https://github.com/p-archana1 ]

LinkedIn : [ linkedin.com/in/archana-prusty-4aa0b827a ]

twitter : [ https://x.com/_archana77 ]

Join the conversation :

Share your own learning experiences or ask questions in the comments.

HAPPY LEARNING!!

THANK YOU!!