-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConstructors_Implementation.py
More file actions
98 lines (83 loc) · 3.09 KB
/
Constructors_Implementation.py
File metadata and controls
98 lines (83 loc) · 3.09 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/python3
# -----------------------------------------------------------------------------
# Script Name: Constructors_Implementation.py
# Experiment: Experiment 3, Program 3.2
# Description: Demonstrates implementation of Constructors (__init__) in Python.
# Shows object initialization with default and custom arguments.
#
# Authors: Amey Thakur
# Repository: https://github.com/Amey-Thakur/OPEN-SOURCE-TECH-LAB
# License: CC BY 4.0
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# STUDENT CLASS DEFINITION
# -----------------------------------------------------------------------------
class Student:
"""
Student class with a Constructor and basic variable management.
"""
# Class Variables
no_of_courses = 5
credits = 25
def __init__(self, r=0, n='', a='', m=0):
"""
Constructor (__init__): Initializes the object state.
Arguments with default values allow flexible object creation.
"""
self.rollno = r
self.name = n
self.addr = a
self.mob = m
print(f">> Student Object Created: {self.name if self.name else 'Unknown'}")
def setval(self, rollno, name, addr, mob):
"""
Method to update object properties after creation.
"""
self.rollno = rollno
self.name = name
self.addr = addr
self.mob = mob
def getval(self):
"""
Returns a tuple of student details.
"""
return self.rollno, self.name, self.no_of_courses, self.credits, self.mob
@classmethod
def setcourses(cls, n):
cls.no_of_courses = n
@classmethod
def setcredits(cls, c):
cls.credits = c
@staticmethod
def is_workday(day):
if day.weekday() == 6:
return False
return True
# -----------------------------------------------------------------------------
# DRIVER CODE
# -----------------------------------------------------------------------------
print("\n--- 1. Constructor Usage ---")
# 1. Object creation with arguments provided to Constructor
# Note: '1' passed as Number, '9822' passed as Number.
s1 = Student(58, 'Mega', 'Mumbai', 9167078027)
print(f"Details s1: {s1.getval()}")
# 2. Object creation with Default Constructor Arguments
s2 = Student()
print(f"Details s2 (Default): {s2.getval()}")
# Modifying s2 values using setval
print("\n--- 2. Updating Object Data ---")
s2.setval('50', 'Amey', 'Mumbai', 9898998989)
print(f"Details s2 (Updated): {s2.getval()}")
# 3. Another Object creation demonstrating flexibility
print("\n--- 3. Multiple Instances ---")
s3 = Student()
s3.setval('51', 'Hasan', 'Mumbai', 9898989898)
print(f"Details s3: {s3.getval()}")
print(f"Memory Address s3: {id(s3)}")
s4 = Student()
s4.setval('57', 'Filly', 'Mumbai', 9898989898) # Same data as s3
print(f"Details s4: {s4.getval()}")
print(f"Memory Address s4: {id(s4)}")
if id(s3) != id(s4):
print(">> s3 and s4 have different memory addresses (distinct objects).")
print("-" * 30)