programming like c, c++, java,python learn in free

Sunday, April 19, 2020

Constructor Concept(Python)


  • 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()
Output:
Constructor exeuction...
Constructor exeuction...
Constructor exeuction...
Method execution...
Share:

0 comments:

Post a Comment

Unordered List

recentposts1
Powered by Blogger.

Text Widget

Featured Post

C programming Question and Answer Day-15

71. Program to check Niven number (Harshad number). #include<stdio.h> #include<conio.h> void main() {     int n, d, a, su...

Search This Blog

recent comments

recentcomments

Labels

[slideshow][technology]

vertical posts

[verticalposts][technology]

business

[business][grids]

ad space

ads 600

vehicles

[cars][stack]
[verticalposts][food]

our facebook page

about us

logo

Jupiter is a magazine responsive Blogger template. It has everything you need to make your blog stand out. This template is fully customizable and very flexible and we believe you will love it as much as we do.

Slide show

[people][slideshow]

health

[health][btop]

recent posts

recentposts1

Subscribe Us

random posts

randomposts2

Blog Archive

Recent Posts

top ads

Unordered List

recent

Pages

Theme Support

[socialcounter] [facebook][#][215K] [twitter][#][115K] [youtube][#][215,635] [rss][#][23M] [linkedin][#][21.5K] [instagram][#][600,300]