-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstructors.py
More file actions
68 lines (37 loc) · 1.59 KB
/
constructors.py
File metadata and controls
68 lines (37 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
class person:
def __init__(self,l,m):
print("Hey i am a person")
self.name = l
self.occ = m
# name = "Pratham"
# occ = "developer"
def info (self):
print(f"{self.name} is a {self.occ}")
a= person("Harry", "HR")
b= person("Hariom", "CA")
# print(a.name)
a.info()
b.info()
# a.name="divya"
# a.occ = "Hr"
# CONSTRUCTOR
# A constructor is a special method in a class used to create and initialize an object of a class. There are different types of constructors. Constructor is
# invoked automatically when an object of a class is created.
# A constructor is a unique function that gets called automatically when an
# object is created of a class. The main purpose of a constructor is to initialize
# or assign values to the data members of that class. It cannot return any value
# Constructors
# other than None.
# Syntax of Python Constructor
# def __init__(self):
# # initializations
# init is one of the reserved functions in Python. In Object Oriented Programming, it is known as a constructor.
# Types of Constructors in Python
# 1. Parameterized Constructor
# 2. Default Constructor
# Parameterized Constructor in Python
# Parameterized Constructor in Python
# When the constructor accepts arguments along with self, it is known as parameterized constructor.
# These arguments can be used inside the class to assign the values to the data members.
# Default Constructor in Python
# When the constructor doesn't accept any arguments from the object and has only one argument, self, in the constructor, it is known as a Default constructor.