Instance and class data

Object-Oriented Programming in Python

Alex Yarosh

Content Quality Analyst @ DataCamp

Core principles of OOP

 

Inheritance:

  • Extending functionality of existing code

Polymorphism:

  • Creating a unified interface

Encapsulation:

  • Bundling of data and methods
Object-Oriented Programming in Python

Instance-level data

class Employee:
    def __init__(self, name, salary):
       self.name = name
       self.salary = salary

emp1 = Employee("Teo Mille", 50000) 
emp2 = Employee("Marta Popov", 65000)
  • name, salary are instance attributes
  • self binds to an instance
Object-Oriented Programming in Python

Class-level data

  • Data shared among all instances of a class
  • Define class attributes in the body of class
class MyClass:
    # Define a class attribute
    CLASS_ATTR_NAME = attr_value


  • "Global variable" within the class
Object-Oriented Programming in Python

Class-level data

class Employee:
  # Define a class attribute
  MIN_SALARY = 30000    #<--- no self. 

def __init__(self, name, salary): self.name = name # Use class name to access class attribute if salary >= Employee.MIN_SALARY: self.salary = salary else: self.salary = Employee.MIN_SALARY
  • MIN_SALARY is shared among all instances
  • Don't use self to define class attribute
  • use ClassName.ATTR_NAME to access the class attribute value
Object-Oriented Programming in Python

Class-level data

class Employee:
  # Define a class attribute
  MIN_SALARY = 30000    

def __init__(self, name, salary): self.name = name # Use class name to access class attribute if salary >= Employee.MIN_SALARY: self.salary = salary else: self.salary = Employee.MIN_SALARY
emp1 = Employee("TBD", 40000)
print(emp1.MIN_SALARY)
30000
emp2 = Employee("TBD", 60000)
print(emp2.MIN_SALARY)
30000
Object-Oriented Programming in Python

Why use class attributes?

 

  • minimal/maximal values for attributes
  • commonly used values and constants, e.g. pi for a Circle class
  • ...
Object-Oriented Programming in Python

Class methods

  • Methods are already "shared": same code for every instance
  • Class methods can't use instance-level data
class MyClass:

  @classmethod                         # <---use decorator to declare a class method

def my_awesome_method(cls, args...): # <---cls argument refers to the class # Do stuff here # Can't use any instance attributes :(
MyClass.my_awesome_method(args...)
Object-Oriented Programming in Python

Alternative constructors

class Employee:
  MIN_SALARY = 30000
  def __init__(self, name, salary=30000):
      self.name = name
      if salary >= Employee.MIN_SALARY:
        self.salary = salary
      else:
        self.salary = Employee.MIN_SALARY
  • Can only have one __init__()

  @classmethod
  def from_file(cls, filename):
      with open(filename, "r") as f:
          name = f.readline()
      return cls(name)

  • Use class methods to create objects
  • Use return to return an object
  • cls(...) will call __init__(...)
Object-Oriented Programming in Python

Alternative constructors

class Employee:
  MIN_SALARY = 30000
  def __init__(self, name, salary=30000):
      self.name = name
      if salary >= Employee.MIN_SALARY:
        self.salary = salary
      else:
        self.salary = Employee.MIN_SALARY

  @classmethod
  def from_file(cls, filename):
      with open(filename, "r") as f:
          name = f.readline()
      return cls(name)        

text file containing one line with the name of the employee

# Create an employee without calling Employee()
emp = Employee.from_file("employee_data.txt")
type(emp)
__main__.Employee
Object-Oriented Programming in Python

Let's practice!

Object-Oriented Programming in Python

Preparing Video For Download...