Introduction

Python is known for its strength as Object Oriented Programming even though Python itself is a multi-paradigm progamming language which support different programming approaches like other design pattern such as procedural, functional and more.

OOP uses the concept of objects and classes. It is a method of restructuring a program by grouping related attributes and behaviors into an object. Let simplify this:

An object has two criteria:

- attributes (character they possess)
- behaviors (actions they perform)

Some refer attributes as state or variables while behaviors also known as method or function.

How to define a class

class Person: pass

person.py

  • To define a class you need to start with class keyword then followed by the name of the class and end with a colon. If we break it down there are 3 parts here referring to our above example script as person.py which is:
    • class to be start with
    • Person a class name with capitalized word/s
    • : end with a colon
  • Anything goes below the class definition will be indented and belongs to the class itself.

Instantiate-an-object

Since we have created our class or we can call it our blueprint of our object. Now we can call our object:

class Person:
    pass

# instantiate an object
obj = Person()

person.py

Adding-instance-attributes-to-a-class

class Person: # instance attributes def __init__(self): pass # instantiate an object obj = Person()

person.py

  • Next line after class defined we create a method start with def keyword. In Python after def will be refer as function but if it is under a class object it known as method. In simpler way to say that classes define functions called methods, which identify the behaviors and actions for an obejct.
  • Inside the class, and __init__ method is a special built-in method in Python or we can call it as initializer that can be used later to instantiate an object.
  • Inside the __init__() which we expanded a () parenthesis after __init__ as compared like above that can be put any number of parameters. However it will always first takes one argument variable called self that refers to the object itself.
  • pass is a placeholder used in Python to indicate some python code will be taking over.

Now let think about the attributes of Person class. Usually when we talk about a person as individual is should have a name, age, gender, status and more. For the sake of simplicity we just put name and age for a start as attributes to class Person. It is time to give the atributes to Person class.

class Person:
    # instance attributes
    def __init__(self, name, age):
        self.name = name
        self.age = age

# instantiate an object
obj = Person()

person.py

Instantiate-an-object-attributes

Since we have created our instance attributes name and age. It is time to create two objects with difference name and age as an examples.

class Person:
    # instance attributes
    def __init__(self, name, age):
        self.name = name
        self.age = age

# instantiate an object
obj = Person()

# instantiate two objects 'Joe' and 'Jane'
joe = Person("Joe", 23)
jane = Person("Jane", 21)

person.py

Next, how to print object attributes

Using . dot-notation to print object attributes

class Person:
    # instance attributes
    def __init__(self, name, age):
        self.name = name
        self.age = age

# instantiate an object
obj = Person()

# instantiate two objects 'Joe' and 'Jane'
joe = Person("Joe", 23)
jane = Person("Jane", 21)

# print object name using `.` or dot-notation
joe.name # output - 'Joe'
joe.age # output - 9

person.py

Core-OOP

There are four core of Object-Oriented-Programming or better known as OOP.

  • Abstraction
  • Encapsulation
  • Inheritance
  • Polymorhism

Different-between-abstraction-vs-encapsulation

Main different between abstraction and encapsulation is

  • Encapsulatin were used to do information hiding or data privacy
  • Abstraction were used to make implementation hiding or method privacy

A simple OOP inheritance example

class Pet(object):

    def __init__(self, name, species):
        self.name = name
        self.species = species

    def getName(self):
        return self.name

    def getSpecies(self):
        return self.species

    def __str__(self):
        return "%s is a %s" % (self.name, self.species)

class Dog(Pet):

    def __init__(self, name, chases_cats):
        Pet.__init__(self, name, "Dog")
        self.chases_cats = chases_cats

    def chasesCats(self):
        return self.chases_cats

class Cat(Pet):
    def __init__(self, name, hates_dogs):
        Pet.__init__(self, name, "Cat")
        self.hates_dogs = hates_dogs

    def hatesDogs(self):
        return self.hates_dogs

pets.py

Source-reference-with-link

Useful reference: