🚀 DevOps & SRE Certification Program 📅 Starting: 1st of Every Month 🤝 +91 8409492687 🔍 Contact@DevOpsSchool.com

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Python Tutorials: Class & Object


"""
# 2 class object instance is at 2 diff location of memory
class Student:
pass
student = Student()
print (student)
new_student = Student()
print (new_student)
"""
# EVOLUTION
"""
students = []
class Student:
def add_student(self, name, student_id=332):
student = {"name": name, "student_id": student_id}
students.append(student)
student = Student()
student.add_student("Mark")
print (students)
"""
# Construtors and Special Methos
# EVOLUTION - Construtors method would be automatically get called when you instantiate a class in backgroup
# We can customize a class instantiate behaviour by modifying Construtors method.
"""
students = []
class Student:
def __init__(self, name, student_id=332):
student = {"name": name, "student_id": student_id}
students.append(student)
mark = Student("Mark")
print (students)
# How to fix this?
print (mark)
"""
# Construtors and Special Methos
# EVOLUTION -
"""
students = []
class Student:
def __init__(self, name, student_id=332):
student = {"name": name, "student_id": student_id}
students.append(student)
def __str__(self):
return "Student"
mark = Student("Mark")
print (students)
print (mark)
"""
# EVOLUTION - Instance and Class Attributes
# Instance Attribute This one.
students = []
class Student:
school_name = "DevOps School"
def __init__(self, name, student_id=332):
self.name = name
self.student_id = student_id
students.append(self)
def __str__(self):
return "Student"
def get_name_capitaize(self):
return self.name.get_name_capitaize()
def get_school_name(self):
return self.school_name
mark = Student("Mark")
print (students)
print (mark)
print(Student.school_name)
students = []
class Student:
school_name = "Springfield Elementary"
def __init__(self, name, student_id=332):
self.name = name
self.student_id = student_id
students.append(self)
def __str__(self):
return "Student " + self.name
def get_name_capitalize(self):
return self.name.capitalize()
def get_school_name(self):
return self.school_name
class HighSchoolStudent(Student):
school_name = "Springfield High School"
def get_school_name(self):
return "This is a High School student"
def get_name_capitalize(self):
original_value = super().get_name_capitalize()
return original_value + "-HS"
james = HighSchoolStudent("james")
print(james.get_name_capitalize())
"""
Create a Class
Class Attribute
The __init__() Method
The self Parameter
Methods
Method1 - ie. CREATE
Instance Attribute
Instance Attribute
Method2 - ie. READ
Instance Attribute
Instance Attribute
Method3 - ie. UPDATE
Instance Attribute
Instance Attribute
Method3 - ie. DELETE
Instance Attribute
Instance Attribute
"""
class Car:
# class attribute
wheels = 4
# initializer with instance attributes
def __init__(self, color, style):
self.color = color
self.style = style
# Calling a Class and it would create OBJECT
c = Car('Sedan', 'Black')
d = Car('Maruti', 'Blue')
# Access attributes
print(c.style) # Sedan
print(c.color) # Black
# Access attributes
print(d.style) # Sedan
print(d.color) # Black
class Car:
# class attribute
wheels = 4
# initializer / instance attributes
def __init__(self, color, style):
self.color = color
self.style = style
# method 1
def showDescription(self):
print("This car is a", self.color, self.style)
# method 2
def changeColor(self, color):
self.color = color
c = Car('Black', 'Sedan')
# call method 1
c.showDescription() # This car is a Black Sedan
# call method 2 and set color
c.changeColor('White')
c.showDescription() # This car is a White Sedan
# Deleting a Attributes
del c.color
# Deleting an Object
del c
Subscribe
Notify of
guest


0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

0
Would love your thoughts, please comment.x
()
x