Table of contents

How to define a class

  1. private, protected, public fields in Python’s class

    By default, all things in class are public. But Python use conventions for defining private, protected, public fields.

    • public field

      The field’s name does not start with underscore _, but its first character is a letter.

      For example:

        class Foo:
            def __init__(self, name):
       = name
            def getName(self):
    • protected field

      In Python, the protected field does not exist. So, in the nature, it is still public field. But we apply below convention for it.

      The field’s name must start with underscore _ character.

        class Foo:
            def __init__(self, name):
                self._name = name
            def _getName(self):
    • private field

      The field’s name must start with the double underscore __ character.

        class Foo:
            def __init__(self, name):
                self.__name = name
            def __getName(self):
  2. Inheritance in Python

    Like C++, Python supports the multiple inheritance. Belows are some problems that we will encounter:

    • Syntax of the multiple inheritance

        class SubClass(BaseClass1, BaseClass2):
            # define its methods
    • From a derived class, to call a method of a base class, use super().methodName().

    • To define a static field in a class

        class Employee:
            # attribute for all employees
            empCount = 0
            def __init__(self, name, salary):
                self._name = name
                self._salary = salary
                Employee.empCount += 1
            # define getter/setter for name, salary fields
            def name(self):
                return self._name
            def name(self, name):
                self._name = name
            def salary(self):
                return self._salary
            def salary(self, salary):
                self._salary = salary
            def displayCount(self):
                print('Total Employee %d' %Employee.empCount)
            def displayEmployee(self):
                print('Name: ', self._name, ', Salary: ', self._salary)
        class Director(Employee):
            def __init__(self, name, salary, grant):
                # add properties 
                Employee.__init__(self, name, salary)
                self._grant = grant
            def grant(self):
                return self._grant
            def grant(self, grant):
                self._grant = grant
            def displayEmployee(self):
                # print('Name: ' +, ', Salary: ', self.salary, ', Grant: ', self.grant)
                print(', Grant: ' + self._grant)
            def displayName(self):
                print("Director's name: ", self._name)
        employee = Employee('Sugar', 600)
        director = Director('Bill Gate', 10000, 'CTO')
        print('The number of employee in company: ' + str(Employee.empCount))
        print('Name of employee: {}'.format(
        print('Salary of director: ' + str(director.salary))
        print('About director: \n')
        print('About employee: \n')
        print('About director: \n')
  3. Some decorators in Python’s class

    • @classmethod decorator

      This decorator that applied to a method will accept its first argument is class name. And It used for factory methods that return class objects for different use cases.

        class Employee:
            somethingGlobal = 'Hello, world'
            def __init__(self, name):
                self._name = name
            def displayGlobalContent(cls):
                print('Its content: ', str(cls.somethingGlobal))
    • @staticmethod decorator

      It define a static method in a class. This method does not receive a specific argument like the class method.

      This method will define a utility task.

        class Employee:
            def show():
                print('Hello, world')
    • @property decorator

      It is used to define the getter method.

How to define abstract class

Belows are some steps that we will follow to define abstract class.

  1. Import the abstract base class module

     import abc
  2. Define an abstract base class in Python 3.x

    Use the metaclass property in the class definition.

     class MyAbsClass(metaclass=abc.ABCMeta):
         def myproperty(self):
         def mymethod(self, value):
     # define an abstract class in Python 2.x
     class MyAbsClass2_x(object):
         __metaclass__ = abc.ABCMeta
  3. Implement the abstract class

     class MyConcreteClass(MyAbsClass):
         def myproperty(self):
             return 10
         def mymethod(self, value):
             assert 10 == 10
     # use in main
     c = MyConcreteClass()

Wrapping up

  • Understanding how to work with class in Python.

  • Take advantage of OOP in Python to implement the GOF’s design patterns.