Python - tutorial - 10/14

classes and objects - inheritance - iterators

Revision:


Python - classes and objects

To create a class, use the keyword "class".

Example 1: create a class named MyClass, with a property named x:

        class MyClass:
            x = 5
    

To create objects, use the class named MyClass.

Example 2: create an object named p1, and print the value of x:

        class MyClass:
            x = 5
        p1 = MyClass()
        print(p1.x) # 5
    

The __init__() function: to understand the meaning of classes we have to understand the built-in __init__() function.

All classes have a function called __init__(), which is always executed when the class is being initiated.
Use the __init__() function to assign values to object properties, or other operations that are necessary to do when the object is being created.
The __init__() function is called automatically every time the class is being used to create a new object.

Example 3: create a class named Person, use the __init__() function to assign values for name and age:

        class Person:
            def __init__(self, name, age):
                self.name = name
                self.age = age
        p1 = Person("John", 36)
        print(p1.name) # John
        print(p1.age)  # 35
    

Object methods: objects can also contain methods. Methods in objects are functions that belong to the object.

Example 4: insert a function that prints a greeting, and execute it on the p1 object:

        class Person:
            def __init__(self, name, age):
                self.name = name
                self.age = age
            def myfunc(self):
                print("Hello my name is " + self.name)
            p1 = Person("John", 36)
            p1.myfunc() # Hello my name is John
    

The self parameter is a reference to the current instance of the class, and is used to access variables that belongs to the class. It does not have to be named "self" , you can call it whatever you like, but it has to be the first parameter of any function in the class.

Example 5: use the words "mysillyobject" and "abc" instead of "self":

        class Person:
            def __init__(mysillyobject, name, age):
                mysillyobject.name = name
                mysillyobject.age = age
            def myfunc(abc):
                print("Hello my name is " + abc.name)
        p1 = Person("John", 36)
        p1.myfunc() # Hello my name is John
    

Modify object properties: you can modify properties on objects like this:

Example 6: set the age of p1 to 40:

        class Person:
            def __init__(self, name, age):
                self.name = name
                self.age = age
            def myfunc(self):
                print("Hello my name is " + self.name)
        p1 = Person("John", 36)
        p1.age = 40
        print(p1.age) # 40
    

Delete object properties: you can delete properties on objects by using the "del" keyword.

Example 7: delete the age property from the p1 object:

        class Person:
            def __init__(self, name, age):
                self.name = name
                self.age = age
            def myfunc(self):
                print("Hello my name is " + self.name)
        p1 = Person("John", 36)
        del p1.age
        print(p1.age)
    

Delete objects: you can delete objects by using the "del" keyword.

Example 8: Delete the p1 object:

       del p1
    

The pass statement: "class" definitions cannot be empty, but if you for some reason have a class definition with no content, put in the pass statement to avoid getting an error.

Example 9:

        class Person:
            pass
    


Python - inheritance

Inheritance allows us to define a class that inherits all the methods and properties from another class.

"Parent class" is the class being inherited from, also called "base class".
"Child class" is the class that inherits from another class, also called "derived class".

Create a parent class (or base class): any class can be a parent class, so the syntax is the same as creating any other class.

Example 10: Create a class named Person, with firstname and lastname properties, and a printname method:

        class Person:
            def __init__(self, fname, lname):
                self.firstname = fname
                self.lastname = lname
            def printname(self):
                print(self.firstname, self.lastname)
            #Use the Person class to create an object, and then execute the printname method:
            x = Person("John", "Doe")
            x.printname() # John Doe
    

Create a child class (or, derived class): to create a class that inherits the functionality from another class, send the parent class as a parameter when creating the child class.

Example 11: create a class named Student, which will inherit the properties and methods from the Person class:

        class Student(Person):
            pass
    

Note: use the "pass" keyword when you do not want to add any other properties or methods to the class. Now the Student class has the same properties and methods as the Person class.

Example 12: use the Student class to create an object, and then execute the printname method:

        class Person:
            def __init__(self, fname, lname):
                self.firstname = fname
                self.lastname = lname
            def printname(self):
                print(self.firstname, self.lastname)
        class Student(Person):
            pass

        x = Student("Alice", "Bollen")
        x.printname() # Alice Bollen
    

Add the __init__() function: we want to add the __init__() function to the child class (instead of the pass keyword).

When you add the __init__() function, the child class will no longer inherit the parent's __init__() function. The child's __init__() function overrides the inheritance of the parent's __init__() function.

Example 13: add the __init__() function to the Student class:

        class Student(Person):
            def __init__(self, fname, lname):
                #add properties etc.
    

To keep the inheritance of the parent's __init__() function, add a call to the parent's __init__() function.

Example 14: add call to to parent's __init__() function:

        class Student(Person):
            def __init__(self, fname, lname):
                Person.__init__(self, fname, lname)
    

Use the super() function: Python also has a super() function that will make the child class inherit all the methods and properties from its parent.

By using the super() function, you do not have to use the name of the parent element, it will automatically inherit the methods and properties from its parent.

Example 15: super() function

        class Student(Person):
            def __init__(self, fname, lname):
                super().__init__(fname, lname)
    

Add properties:

Example 16: add a property called graduationyear to the Student class:

        class Student(Person):
            def __init__(self, fname, lname):
                super().__init__(fname, lname)
                self.graduationyear = 2019
    

Example 17: add a year parameter, and pass the correct year when creating objects:

        class Student(Person):
            def __init__(self, fname, lname, year):
                super().__init__(fname, lname)
                self.graduationyear = year

        x = Student("Alice", "Bollen", 2019)
    

Add methods:

Example 18: add a method called welcome to the Student class:

        class Student(Person):
            def __init__(self, fname, lname, year):
                super().__init__(fname, lname)
                self.graduationyear = year

            def welcome(self):
                print("Welcome", self.firstname, self.lastname, "to the class of", self.graduationyear)
    


Python - iterators

An iterator is an object that contains a countable number of values. An iterator is an object that can be iterated upon, meaning that you can traverse through all the values. Technically, in Python, an iterator is an object which implements the iterator protocol, which consist of the methods __iter__() and __next__().

Iterator vs iterable: lists, tuples, dictionaries, and sets are all iterable objects. They are iterable containers which you can get an iterator from. All these objects have a iter() method which is used to get an iterator.

Example 19: return an iterator from a tuple, and print each value:

        mytuple = ("apple", "banana", "cherry")
        myit = iter(mytuple)
        print(next(myit)) # apple
        print(next(myit)) # banana
        print(next(myit)) # cherry
    

Even strings are iterable objects, and can return an iterator.

Example 20: strings are also iterable objects, containing a sequence of characters:

        mystr = "banana"
        myit = iter(mystr)
        print(next(myit)) # b
        print(next(myit)) # a
        print(next(myit)) # n
        print(next(myit)) # a
        print(next(myit)) # n
        print(next(myit)) # a
    

Looping through an iterator: you can also use a "for" loop to iterate through an iterable object. The for loop actually creates an iterator object and executes the next() method for each loop.

Example 21: iterate the values of a tuple:

        mytuple = ("apple", "banana", "cherry")
        for x in mytuple:
            print(x)
    

Example 22: iterate the characters of a string:

        mystr = "banana"
        for x in mystr:
            print(x)
    

Create an iterator: to create an object/class as an iterator you have to implement the methods __iter__() and __next__() to your object.

All classes have a function called __init__(), which allows you to do some initializing when the object is being created.
The __iter__() method acts similar, you can do operations (initializing etc.), but must always return the iterator object itself.
The __next__() method also allows you to do operations, and must return the next item in the sequence.

Example 23: create an iterator that returns numbers, starting with 1, and each sequence will increase by one (returning 1,2,3,4,5 etc.):

        class MyNumbers:
            def __iter__(self):
                self.a = 1
                return self
            def __next__(self):
                x = self.a
                self.a += 1
                return x
        myclass = MyNumbers()
        myiter = iter(myclass)
        print(next(myiter)) # 1
        print(next(myiter)) # 2
        print(next(myiter)) # 3
        print(next(myiter)) # 4
        print(next(myiter)) # 5
    

StopIteration: to prevent the iteration to go on forever, we can use the StopIteration statement. In the __next__() method, we can add a terminating condition to raise an error if the iteration is done a specified number of times.

Example 24: stop after 20 iterations:

        class MyNumbers:
            def __iter__(self):
                self.a = 1
                return self
            def __next__(self):
                if self.a <= 20:
                x = self.a
                self.a += 1
                return x
                else:
                  raise StopIteration
            myclass = MyNumbers()
            myiter = iter(myclass)
            for x in myiter:
                print(x)