Classes and Instantiation

Date: February 3rd 2016
Last updated: February 5th 2017

A class provides a template for information to be entered, such as an empty registration form. When you fill in the form, you are instantiating the class. Another example - imagine a blue print of an animal. An animal can do certain things in common like moving, making a sound or giving birth. Rather than defining these things over and over again we could do it once and then build on top of it. Once the blueprint has been built, the animal class could be applied to a cat, dog, lion etc. The differences come in when we define the behaviour for each of these animals.

A simple class that passes:

class MyClass(object):
    """
    Note all classes inherit from object. Always include 'object'
    when using python2.
    """
    pass

# instantiate an object on MyClass
MyClass.x = 36
MyClass.x
# returns: 36

# classes are also objects
MyClass
# returns: <class '__main__.myclass'>

Example class that holds name and address:

class MyClass(object):
    """
    Example class 
    """
    name = "P.Sherman"
    address = "42 Wallaby Way"

MyClass.name #returns: 'P.Sherman'
MyClass.address #returns: '42 Wallaby Way'

Adding initial attributes:

class MyClass(object):
    """
    Example initialization of a single attribute.
    """

    # initialise using pythons special method: __init__()
    def __init__(self, x):
        # this means myclass needs 'self' and 'x'
        # self is set automatically, leaving 'x' to be defined
        self.x = x

# instantiate MyClass
me = MyClass(56)

me.x
# returns: 56

Including attributes not as arguments:

class MyClass(object):
    """
    Example initialization of empty data structures.
    Creates an empty list and an empty dictionary.
    """

    def __init__(self, x):
        self.x = x
        self.mylist = []
        self.mydict = {}

me = MyClass(56)
me.mylist #returns: []
me.mydict #returns: {}

Adding extra function to a class:

class MyClass(object):
    """
    Example initialization of empty data structures. 
    Creates an empty list and an empty dictionary.
    """
    def __init__(self, x):
        self.x = x
        self.mylist = []
        self.mydict = {}

    def double(self):
        """doubles x and appends to class list"""
        # multiply x provided by the user by 2
        # notice self as a function argument
        self.x = self.x * 2
        self.mylist.append(self.x)

# instantiate class
me = MyClass(2)

me.list #returns: []
me.double()  
me.list #returns: [4] 
me.double()  
me.list #returns: [4, 8]
me.double()  
me.list #returns: [4, 8, 16]

Omit self to keep variables in local scope:

class MyClass(object):
    def __init__(self, x):
        self.x = x

    def double(self):
        """doubles x and appends to class list"""
        # multiply x provided by the user by 2
        # notice self as a function argument
        y = self.x * 2
        return y

# instantiate class
me = MyClass(2)
me.x #returns: 2
me.y #returns: AttributeError: 'MyClass' object has no attribute 'y'
me.double()  #returns: 4

results matching ""

    No results matching ""