Know to work with classes and objects in python.

Classes vs Objects vs Methods

  • Class is a blueprint of an object.
  • Each class has its own attributes and methods (its behaviors).
  • Many objects may have the same class. They have attributes and methods defined in the class.

A class student with:

  • Attributes: name, marks and
  • Methods: take_exams(), graduate().

Objects john_doe and jane_doe defined from class student will have:

  • Their names: john_doe.name and jane_doe.name.
  • Their marks: john_doe.marks and jane_doe.marks.
  • Their behaviors: john_doe.take_exams() and jane_doe.graduate(), …

Import local class

Suppose that we have a folders/files structure like below,

# ORIGINAL STRUCTURE
popai/
  processings/
    a.py # contains class ABC
    test/
      b.py
  lib/
    c.py # contains class XYZ
# UPDATED STRUCTURE
popai/
  __init__.py
  processings/
    __init__.py
    a.py # contains class ABC
    test/
      __init__.py
      b.py
  lib/
    c.py # contains class XYZ

We want import both classes ABC and XYZ,

# b.py
from popai.processings.a import ABC

# a.py
from popai.lib.c import XYZ

Just add __init__.py like in the right box above.

Some errors may occur,

ValueError: attempted relative import beyond top-level package