Table of contents
Object-oriented programming revolves around everything being an object which are instances of classes. In Python, everything is an object, with methods. For example, an object of class 'str' has methods like str.upper(), str.lower(). An object of type 'list' has methods like list.append()
Class
It is like a template that defines the structure and behaviour of an object.
Object
An object is an instance of a class defined by attributes. Every instance creates a different object. See the example below.
class Dog:
pass
#Creating instances of Dog
dog1 = Dog()
dog2 = Dog()
#Checking if the instances are objects
print(isinstance(dog1, object)) #prints True
print(isinstance(dog2, object)) #prints True
#Checking if the instances are the same object
print(dog1 is dog2) #prints False
#The instances are objects but they are different objects
Attributes
Instance attributes: These are variables defined within the class methods, they are the characteristics of an object which vary across different instances.
Class attributes: These are variables defined within the class but outside any defined methods and are shared by all instances of a class.
Methods
These are basically functions defined within the class.
The constructor method or the ` _ _ init _ _( )` function instantiates a class object and is used to initialize an individual object its attributes.
class Dog:
sound = "woof" #Class attribute (defined outside a method)
def __init__(self, param1, param2, ....):
self.param1 = param1 #Instance attribute (defined within a method)
Instance methods: These are functions defined within the class and are used to access and perform actions on the instances. They have the 'self' parameter to reference the current object.
class Dog:
sound = "woof" #Class attribute (defined outside a method)
def __init__(self, param1, param2, ....):
self.param1 = param1 #Instance attribute (defined within a method)
def get_param1(self) #instance method used to access the attribute param1
return self.param1
Class methods: These are functions used to access and perform actions on class attributes only. They are called on the class itself not the objects. They have 'cls' as the first parameter.
Getter and Setter Methods: Getter and setter methods are used to get and set the values of private attributes. They provide controlled access to the attributes and can perform additional validation or actions when getting or setting the values.
I managed to write some lines of code with comments to show the basic usage of a class.
class Dog: #Define Dog class
sound = "woof" #class Attribute
#Constructor method to initialize the Dog object and instance attributes
def __init__(self, name, age):
self.name = name #Instance attribute
self.__age = age #Private Instance attribute
#Instance method
def get_name(self):
return self.name #Return the value of the 'name' attribute
#Getter method
def get_age(self):
return self.age #Return the value of the 'age' attribute
#Setter Method
def set_age(self, age):
self.age = age #Set the value of the 'age' attribute to the provided age
#Class method
def get_sound(cls, sound):
Dog.sound = sound
dog = Dog("Bark", 3) #An instance of the Dog class with name "Bark" and age 3
d = dog.get_name() #Get the name of the dog using the get_name() method and assign it to variable 'd'
#Set the age of the dog to 23 using the set_age() method
dog.set_age(23)
Dog.sound = "Wrrrrr" #Changing the class attribute
#Print the name and age of the dog and the sound it makes
print(f"{d} is {dog.get_age()} years old and goes {Dog.sound}")
references: Chatgpt and OOP - For beginners Youtube Tutorial