Object Oriented




Defining Class

Python 2.7
class Person:

  def __init__(self, name, age):
      self.name = name
      self.age = age
  def greet(self):
    print("Hi! I'm " . self.name)

Class is a blueprint from which Objects are created.

Class represents a set of properties (state) and methods (behaviors) that are common to all objects created from it.


Instantiate Class

Python 2.7
person1 = Person('Bob', 25)

person2 = Person('Sarah', 21)

In order to create a new object out of a class, you can call the Class with arguments, similar to when you call a function.

Object is giving a reality to a class.

Class is like a blueprint for a house and Object is like an actual house created from that blueprint.