xxxxxxxxxx
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def display_info(self):
print(f"Name: {self.name}, Age: {self.age}")
# Creating objects/instances of the Person class
person1 = Person("John Doe", 25)
person2 = Person("Jane Smith", 30)
# Accessing object properties and calling object methods
person1.display_info() # Output: Name: John Doe, Age: 25
person2.display_info() # Output: Name: Jane Smith, Age: 30
xxxxxxxxxx
class Muggle:
def __init__(self, age, name, liking_person):
self.age = age
self.name = name
self.likes = liking_person
Vernon = Muggle(52, "Vernon", Petunia)
Petunia = Muggle(49, "Petunia", Vernon)
xxxxxxxxxx
Class Definition Syntax:
class ClassName:
# Statement
Object Definition Syntax:
obj = ClassName()
print(obj.atrr)
Suppose, there are two employees at Educative, Mark and Chris. The properties of Mark and Chris are given in the image below:
Properties
Properties are variables that contain information regarding the object of a class. An employee object will have an ID, a salary, and the department as its properties. New properties can be added to become a part of an object of the employee class.
Attributes are also referred to as properties or members. For consistency, we will be using properties.
Methods
Methods are like functions that have access to properties (and other methods) of a class. Methods can accept parameters and return values. They are used to perform an action on an object of a class. In the example above, we have tax() and salaryPerDay() as class methods.