Objects

The object is an entity that has a state and behavior associated with it. It may be any real world entity like: mouse, keyboard, pen, table, chair, etc.

Object consist of:

To understand the state, behavior and identity let us take the example of the dog:-

Creating an object: obj = Dog()

Python Self

Use of self : When working with the classes in python, the term self refers to the instance of the class that is currently being used. It is customary to use "self" as the first parameter in instance methods of a class. Whenever you call a method of an object created from a class, the object is automatically passed as the first argument using the self parameter. This enables you to modify the objects properties and executes takes unique to that particular instance.

Example:

class myNumber: def __init__(self, value): self.value = value def printValue(self): print(self.value) obj1 = myNumber(30) obj1.printValue() """ Output: 30 """

Self Parameter When we call a method of this object as myObject.method(arg1, arg2), this is automatically converted by python into myClass.method(myObject, arg1, arg2) - this is all the special self is about.

class cambridgeCSLectures: def __init__(self, name, lecturer, course, year, semester): self.name = name self.lecturer = lecturer self.course = course self.year = year self.semester = semester def displayDetails(self): print("Hello, My name is " + self.name + " as a " + self.lecturer + " lecturer of " + self.course + " of " + self.year + " of " + self.semester + " semester ") obj = cambridgeCSLectures("Vayishu", "Computer Science", "Introduction to Python Programming", "2024", "2nd") obj.displayDetails() """ Output: Hello, My name is Vayishu as a Computer Science lecturer of Introduction to Python Programming of 2024 of 2nd semester """

The self parameter doesn't call it to be self, you can use any other name instead of it.

=> The __init__ method is similar to constructor in CPP and Java. It is run as soon as the object os a class is instantiated. The method is useful to do any initialization you want to do with your object.

Class and instance variable or attribute

class Dog: animal = 'dog' # class variable def __init__(self, name, breed): # init method or constructor self.name = name # instance variable 1 self.breed = breed # instance variable 2 def setColor(self, color): self.color = color # instance variable 3 def getDetails(self): print("I have my pet and his name is " + self.name + " and he belongs to the breed of " + self.breed) # Retrieves an instance variable 1 and 2 def getColor(self): return self.color # Retrieves an instance variable 3 petDog = Dog("Jimmy", "Pug") petDog.setColor("White") petDog.getDetails() color = petDog.getColor() print("Color: ", color) """ Output: I have my pet and his name is Jimmy and he belongs to the breed of Pug Color: White """

Define a class in Python? To define a class in python, use the class keyboard foloowed by the class name and a colon.

class myClass: def __init__(self, arg1, arg2): self.arg1 = arg1 self.arg2 = arg2 def someMethod(self): # Method definitions pass

Example-1 and Example-2 are in the folder of the ClassWork Programs and into the folder of the Classes and Objects.