1. Procedural Programming#

We normally code as a sequence of steps:

  1. Download the data

  2. Process the data

  3. Visualize it

The more data we have, the more functionality we might create, and the harder it is to think about as just a sequence of steps.

2. Object-Oriented Programming#

An object is a data structure, which contains information about state and behavior.

For example, an object representing a person can have a certain name, phone number, andemail associated with them, and behaviors like callPerson and emailPerson.

Instead of thinking of person data separately from person actions, we think of them as one unit representing a person. This is called encapsulation.

2.1. Classes#

Classes describe the possible states and behaviors that every object of a certain type could have.

For example. We can say that “every person will have a name, phone number and email, and will be able to receive calls and emails. Bingo! you just defined a class.

See a Person Class summary built in cards for better visualization.

Person 1

name:

phone-number:

email:

Person 2

name:

phone-number:

email:

Note

Now we can talk about any person in a unified way.

2.2. Objects#

  • Everything in Python is an object

  • Every object has a class

Object

Class

5

int

“Hello”

str

pd.DateFrame()

DataFrame

  • You can use type() to find the class

2.2.1. Example 1#

  • Let’s create a simple class called Car and lets display it:

    Tip

    Click on Run code button to execute the code. Then, you can type inside the cell, if needed. =)

    class Car: pass
    Car
    

    __main__.Car = This means that Car is in the module main

    Note

    The word Car is a variable that references the class object from the memory address.

  • If we want to Instantiate, or represent, a class, we have to add the parenthesis.

    Car()
    

    Important

    The Object has the ID 0xxxxxxxxx, which is an instance/reference of the class Car from the module main.

  • Let’s check the type of an instance by doing the following:

    type(Car())    
    

    Note

    When we call the instance Car() inside type(), the object we are creating while calling type() is the type = Car.

  • Let’s now check the type of this class:

    type(Car) 
    

    Note

    Here, the class Car, is a type of type

  • On Python3, every class is a subclass of an object.

    issubclass(Car,object) 
    

    True

    class Car(object): pass is the same as class Car: pass. In other words, we are saying that Car is a class that inherits from object.

Classes and objects both have attributes and methods, but the difference is that a class is an abstract template, while an object is a concrete representation of a class.

2.3. Attributes and Methods#

Classes contain information about state and behavior. State information is contained in attributes, and behavior in methods.

  • Attributes (or states) are represented by variables, such as numbers, strings , or tuples.

  • Methods (or behaviors) are represented by functions.

Both are accessible from an object using the dot syntax. For example:

# Call attribute
obj.my_attribute

# Call method
obj.my_method()

Tip

You can type dir() to list all attributes and methods of and object