Python training exercise 11

From BITS wiki
Jump to: navigation, search

Introduction

The code we've been writing so far has been a sequential set of commands and functions where variables have to be passed around from function to function whenever required. Python is, however, inherently an object-oriented programming language. This means that you can create objects that are self-sustained building blocks of code or data which you can combine to form more complex programs.

In fact we've been using objects all along; strings are objects, as are lists and dictionaries - the methods we've been using on them (for example myList.sort()) are part of these objects.

The concept of objects is not very intuitive, but we'll give you a taste here so you can see how powerful it is in practice.

Exercises

Creating a class, the template for an object

First you have to create a template for your object; this determines what an object can do once you start to use it. This template is called a class. Consider this example

# Define the MyMathOperations class
# Note that we start the class name with an uppercase letter
# to distinguish it from normal variables
class MyMathClass:
 
  def add(self,x,y):
 
    print(x + y)
 
  def subtract(self,x,y):
 
    print(x - y)
 
  def multiply(self,x,y):
 
    print(x * y)

So within a class you define the methods add(), substract() and multiply(). The argument list for each of these has to start with self - this means it belongs to this class, and can be called from it using the .add() syntax as we used for strings, lists and dictionaries.

Using an object

When you execute the above example, nothing happens. This is because the class is not instantiated to become a working object. It's very easy to do this and then use the class; add these lines to the above example:

if __name__ == '__main__':
 
  # Instantiate the object from the class
  myMathObject = MyMathClass()
 
  myMathObject.add(3,5)
  myMathObject.subtract(5,4)
  myMathObject.multiply(9,3)

One big immediate advantage of doing this is that you can group similar functions (methods) together, and only have to call up the class to be able to execute them (you don't have to import all of the functions separately).

You can also connect variables to a class. You can do this when you instantiate (initialise) the class, or change/add new ones later on. Consider this modified example:

class MyNewMathClass:
 
  # This method is called when initialising the class
  def __init__(self,x,y):
 
    self.x = x
    self.y = y
 
  def add(self):
 
    return self.x + self.y
 
  def subtract(self):
 
    return self.x - self.y
 
  def multiply(self):
 
    return self.x * self.y
 
  def doSomeOperations(self):
 
    print("Numbers {} and {}".format(self.x,self.y))
    print("  Adding...", self.add()
    print("  Subtracting...", self.subtract()
    print("  Multiplying...", self.multiply()
    print
 
if __name__ == '__main__':
 
  # Instantiate the object from the class
  myMathObject = MyNewMathClass(3,5)
 
  myMathObject.doSomeOperations()
 
  myMathObject.x = 5
  myMathObject.y = 4
 
  myMathObject.doSomeOperations()



Combining classes

The object-oriented way of programming becomes really powerful when you start to combine classes; in programming speak you can create a subclass that inherits the methods, variables, ... from one or several other classes (the superclasses). This way you can use classes as 'building blocks' to create more complex programs very quickly:

class MyInputClass:
 
  def getUserInput(self,questionString):
 
    userInput = None
 
    while not userInput:
      userInput = input(questionString)
 
    return userInput
 
  def getUserString(self):
 
   return self.getUserInput("Please enter a string: ")
 
class MyStringClass:
 
  # This is a modified version of the class introduced earlier
  def setStringsAndCreateStringSets(self,string1,string2):
 
    self.string1 = string1
    self.string2 = string2
 
    # Already make sets for comparisons later...
    self.stringSet1 = set(string1)
    self.stringSet2 = set(string2)
 
  def getSharedCharacters(self):
 
    return self.stringSet1.intersection(self.stringSet2)
 
class UserStringSharedCharacters(MyInputClass,MyStringClass):
 
  def getSharedCharactersFromUserStrings(self):
 
    string1 = self.getUserString()
    string2 = self.getUserString()
 
    self.setStringsAndCreateStringSets(string1,string2)
    sharedCharacters = self.getSharedCharacters()
 
    print ("Shared characters between '{}' and '{}' are {}".format(string1,string2,', '.join(sharedCharacters)))
 
if __name__ == '__main__':
 
  userStringSharedChars = UserStringSharedCharacters()
  userStringSharedChars.getSharedCharactersFromUserStrings()


Back to main page