Python – Object Oriented Programming – Classes and Objects

Object Oriented Programming – Intro

What’s OOP ?

Procedural vs Object Oriented

  • Procedural programming :
    • sequential coding
    • OK for Data Analysis
  • Object Oriented
    • Coding as interaction for object
    • Frameworks and tools
    • usability

Terminology

  1. Object = state + behaviour  ==> a FIlou a dog has 4 legs and a tail, and can bark and fetch a ball
  2. Encapsulation = handling data with code operating on it
  3. Classes are like blueprints for possible states & behaviour
    1. class = abstract pattern  (example: dog)
    2. object = particular representation of a class  ( = Filou )

Object in Python

Everyhting is an object in Python  :

    • 5 –> class : int
    • “Hello” –> class : str
    • np.mean() –> class : function

  • in case of Filou –> type(Filou) would be class Dog

 

Attributes and Methods

State —  Attributes

  • attribute — variable(s) — obj.my_attribute
  • in case of Filou –> Filou’s attributes = ‘4 legs’, ‘1 tail’

Behavior — Methods

  • method — function() — obj.my_method()
  • In case of Filou –> methods are ‘barking’, ‘fetching balls’

Listing all attributes and methods

  • listing of Filou –> dir(Filou) –> [‘legs’,’tail’,’bark’,’fetch_balls’ ]
  • help() to see documentation on object –> provided the class documentation filled in

 

Class anatomy : basics

 

Basic Class

  • class <>: starts a class definition
  • code inside class is indented
  • use pass to create an empty class

  • Use Classname() to create an object of class ClassName

 

Add methods to a class

  • method def (inition) = function within a class
  • use self as the 1st argument in method definition

  • ignore self when calling method on an object –> in this case name is the parameter for identify()

What is self ?

  • classes are templates, how to refer to data of a particular object ?
  • self is a stand-in for a particular object used in class definition
  • self should be the first argument of any method .
  • Python will take care of self when the method is called from an object
    • cust.identify("Marie") is being interpreted as Customer.identify(cust, "Marie")

Attributes in a class

  • encapsulation –> bundling data with methods that operate on the data.
    • Customer name should be an attribute instead of an parameter through a method
  • attributes are created by assignment in methods .

Add an attribute to a class

New version with identify

Exercise

Class anatomy  : The __init__ constructor

Methods and attributes

  • Methods are function definitions within a class
  • self as the 1st argument
  • Attributes defined by assignment
  • Referral to attributes in class via self.___

Constructor __init__()

  • Add data to object when creating it ?
  • Constructor __init__() method is called every time an object is created

  • with default parameters

Attributes in Methods

Attributes in constructor

  • easier to know all attributes
  • attributes are created when the object is created
  • more usable and maintainable code 

Best Practices

  1. Initialize attributes in __init__()
  2. Naming
    1. CamelCase for class
    2. lower_snake_case for functions and attributes
  3. Keep self as self
    1. readable –> self can be renamed but less readable
  4. Use docstrings

 

Exercise on self / __init __

Exercise