Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




PYTHON - CLASSES & OBJECTS


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 .


As we all know, we being Humans, have a common behavior of speaking and eating.

Now, just imagine, when God had created the Human Beings, God had prepared a blue print stating,all Humans will have a common 'behavior' of speaking and eating.

java_Collections

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.


i.e. Say, a guy named 'John' who speaks 'English' and eats 'Burger' is one Person.
And a girl named 'Rakhi' who speaks 'Hindi' and eats 'Rice' is a different Person.

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.


java_Collections

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' .


java_Collections

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.


Example :



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()


Output :



  Rakhi eats Rice
  Rakhi speaks Hindi
  John eats Burger
  John speaks English

So , in the above example, we have created a class named 'Human' .


class 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 ':' .


java_Collections

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).


Note : We will explain 'self' keyword in a while. But remember, 'self' keyword is a mandatory one. If you miss it in the arguments, your code will end up with an error.

Similarly , we have the 'speak(...)' behaviour/method.


def speak(self):
    print(self.name," speaks ",self.language);

So , we have created the class 'Human' .


java_Collections

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.


human1 = Human()

Creating an object is also pretty straight forward. You give an object name, 'human1' and initialise with the class name with brackets.


java_Collections

So, 'human1' object is created. And 'human1' object is for the guy named 'John' who speaks 'English' and eats 'Burger' .


human1.name = "John"
human1.food = "Burger"
human1.language = "English"

java_Collections

Then we create the next object 'human2' .


human2 = Human()

And 'human2' object is for the girl named 'Rakhi' who speaks 'Hindi' and eats 'Rice' .


human2.name = "Rakhi"
human2.food = "Rice"
human2.language = "Hindi"

java_Collections

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( )' .


human1.eat()

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.


human1.eat( )

And the 'print' statement,


print(self.name," eats ",self.food)

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 ,


Rakhi eats Rice

And exactly same logic applies to the other lines as well.


human1.speak()
human2.eat()
human2.speak()

Caution : Java, C++ programmers might find the class a little different. With no getters and Setters, no private data members. Well! This this is Pythons way. So, get used to it.

Deletion of an Object


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.


Example :



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)


Output :



  print(human1.name)
 NameError: name 'human1' is not defined

And all we have done is ,


  1. Declared the 'human1' object.

    human1 = Human( )
  2. And assigned values to it.

    human1.name = "John"
    human1.food = "Burger"
    human1.language = "English"
  3. Then we deleted the 'human1' Object using the 'del' keyword.

    del human1
  4. So , when we try printing the 'name' attribute of 'human1' Object.

    print(human1.name)
  5. We get the below error .

     print(human1.name)
    NameError: name 'human1' is not defined

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.


human1 = Human()
human1.name = "John"
human1.food = "Burger"
human1.language = "English"

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.