Anything and everthing you see around, can be said to be a 'class' . Be it a Car or a Tree or even a Human.
A 'class' can be described as a blueprint which says how an 'object' is going to behave once it is created. And a 'class' has a behaviour and a state.
Let us simplify with the below example .
So , speaking and eating behaviour would be common in all Human Beings. But each Human Being will have a property, that would makes them unique.
So , let's say, God had also made this rule called along with the Behaviour, a Human Being should also have property/state( i.e. Name, language, food ) that will make them unique.
Now , if we compare the above 'Human' as a class. The 'name', 'food' and 'language' are called as property/state that will be unique for a Human Being.
And the Eat and Speak are 'behaviour' that will be common to all Human Beings.
Now , let us create two humans out of the above class. i.e. A guy named 'John' who speaks 'English' and eats 'Burger' and a girl named 'Rakhi' who speaks 'Hindi' and eats 'Rice' .
So , 'Human' is a 'Class' that is just a Blue print and doesn't exist physically.
And 'human1' and 'human2' are called as 'Objects' that exist physically ( i.e. The John and Rakhi are humans that have physical existence) and are derived from the above class 'Human' .
Now , let us see, how can we implement 'Classes' and 'Objects' in Python.
class Human: def eat(self): print(self.name," eats ",self.food) def speak(self): print(self.name," speaks ",self.language) human1 = Human() human1.name = "John" human1.food = "Burger" human1.language = "English" human2 = Human() human2.name = "Rakhi" human2.food = "Rice" human2.language = "Hindi" human1.eat() human1.speak() human2.eat() human2.speak()
So , in the above example, we have created a class named 'Human' .
The syntax of declaring a 'class' is quite simple, class name( i.e. 'Human' ) followed by the keyword 'class' and the statement ends with a colon ':' .
And as we have seen know humans have a common 'behavior' of speaking and eating.
And the 'behaviours' in Python can be represented by 'Functions' with a keyword 'self' in its arguments.
And next we have the 'eat(...)' behaviour/Function. Whenever a 'Function' is defined inside a class,it is called as 'method' .
def eat(self): print(self.name," eats ",self.food);
And in the 'print' statement we have the properties/state 'name' and 'food' (Detail explanation later).
Similarly , we have the 'speak(...)' behaviour/method.
def speak(self): print(self.name," speaks ",self.language);
So , we have created the class 'Human' .
Now , just remember, a class has no existence of its own. It only comes to effect once an 'Object' is created out of it.
And we will see next, how can we create an 'Object' of 'Human' class.
Creating an object is also pretty straight forward. You give an object name, 'human1' and initialise with the class name with brackets.
So, 'human1' object is created. And 'human1' object is for the guy named 'John' who speaks 'English' and eats 'Burger' .
Then we create the next object 'human2' .
And 'human2' object is for the girl named 'Rakhi' who speaks 'Hindi' and eats 'Rice' .
So , the 'Objects', 'human1' and 'human2' of the class 'Human' has the above values.
Now , if we want to get the values of the 'Objects', 'human1' and 'human2' . We need to call the methods, 'eat( )' and 'speak( )' .
So , the 'eat( )' method of 'human1' object is called.
def eat(self): print(self.name," eats ",self.food)
So , the 'eat( )' method has an argument named 'self' . 'self' is actually the calling 'Object' itself.
And in this case, 'self' is referring to 'human1' Object. As the 'eat( )' method is called by the 'human1' Object.
And the 'print' statement,
Goes to the 'human1' object and finds that, 'self.name' is 'John' (Since, 'self' is referring to the 'human1' object now) and 'self.food' is referring to 'Burger' .
So we get the output ,
And exactly same logic applies to the other lines as well.
So , we have seen, how to create an Object. Now , let us see, how can we delete it.
An object can be deleted using the 'del' keyword. Let us see it below.
class Human: def eat(self): print(self.name," eats ",self.food) def speak(self): print(self.name," speaks ",self.language) human1 = Human() human1.name = "John" human1.food = "Burger" human1.language = "English" del human1 print(human1.name)
And all we have done is ,
As the Object is already deleted .
Now , an important thing to note is, although 'name', 'food' and 'language' were supposed to be a part of the class. But there is no track of them inside the class.
In simple words , they were scattered across the methods, 'eat( )' and 'speak( )' .
class Human: def eat(self): print(self.name," eats ",self.food) def speak(self): print(self.name," speaks ",self.language)
Also they were initialised outside the class.
This becomes a challenge for a programmer, when the attributes in a class are more.
To solve this , we will be looking at Constructors or the '__init__( )' method in the next tutorial that resolves this problem.