Class anatomy: the __init__ constructor

Introduction to Object-Oriented Programming in Python

George Boorman

Curriculum Manager, DataCamp

Methods and attributes

  • Methods are function definitions within a class
  • self as the first argument
  • Define attributes by assignment
  • Refer to attributes in class via self.___
  • Calling lots of methods could become unsustainable!
class MyClass:
    # function definition in class
    # first argument is self
    def my_method1(self, other_args...):
        # do things here

def my_method2(self, my_attr): # attribute created by assignment self.my_attr = my_attr ...
Introduction to Object-Oriented Programming in Python

Constructor

  • Add data to object when creating it
  • Constructor __init__() method is called every time an object is created
    • Called automatically because of __methodname__ syntax
class Customer:
    def __init__(self, name):
        # Create the .name attribute and set it to name parameter
        self.name = name
        print("The __init__ method was called")
Introduction to Object-Oriented Programming in Python

Constructor

# __init__ is implicitly called 
cust = Customer("Lara de Silva")   
print(cust.name)
The __init__ method was called
Lara de Silva
Introduction to Object-Oriented Programming in Python

Attributes in methods

class MyClass:
    def my_method1(self, attr1):
        self.attr1 = attr1
        ...

    def my_method2(self, attr2):        
        self.attr2 = attr2
        ...

obj = MyClass() # attr1 created obj.my_method1(val1) # attr2 created obj.my_method2(val2)

Attributes in the constructor

class MyClass:
    def __init__(self, attr1, attr2):
        self.attr1 = attr1
        self.attr2 = attr2
        ...

# All attributes are created obj = MyClass(val1, val2)
  • Generally we should use the constructor
  • Attributes are created when the object is created
  • More usable and maintainable code
Introduction to Object-Oriented Programming in Python

Add arguments

class Customer:
    # Add balance argument
    def __init__(self, name, balance): 

self.name = name # Add the balance attribute self.balance = balance print("The __init__ method was called")
Introduction to Object-Oriented Programming in Python

Add parameters

# __init__ is called
cust = Customer("Lara de Silva", 1000)
print(cust.name)
print(cust.balance)
The __init__ method was called
Lara de Silva
1000
Introduction to Object-Oriented Programming in Python

Default arguments

class Customer:
    # Set a default value for balance
    def __init__(self, name, balance=0):
        self.name = name
        # Assign the new attribute
        self.balance = balance
        print("The __init__ method was called")
Introduction to Object-Oriented Programming in Python

Default arguments

# Don't specify the balance explicitly
cust = Customer("Lara de Silva")

print(cust.name) # The balance attribute is created anyway print(cust.balance)
The __init__ method was called
Lara de Silva
0
Introduction to Object-Oriented Programming in Python

Best practices

1. Initialize attributes in __init__()
Introduction to Object-Oriented Programming in Python

Best practices

1. Initialize attributes in __init__()
2. Naming

CamelCase for classes, lower_snake_case for functions and attributes

Introduction to Object-Oriented Programming in Python

Best practices

1. Initialize attributes in __init__()
2. Naming

CamelCase for class, lower_snake_case for functions and attributes

3. Keep self as self
class MyClass:
    # This works but isn't recommended
    def my_method(george, attr):
        george.attr = attr
Introduction to Object-Oriented Programming in Python

Best practices

1. Initialize attributes in __init__()
2. Naming

CamelCase for class, lower_snake_case for functions and attributes

3. self is self
4. Use docstrings
class MyClass:
    """This class does nothing"""
    pass
Introduction to Object-Oriented Programming in Python

Let's practice!

Introduction to Object-Oriented Programming in Python

Preparing Video For Download...