Preview

03-Use of Classes

 1. A class is a software entity that has ...

  internal data and a set of methods or functions that act upon that data.

  internal classes with objects that can be set to private

  internal methods with functions that are attributed to other objects

  internal methods with hidden attributes

 2. Classes are

  the blueprints of an object that cannot be reused

  the blueprints of an object that can be reused.

  the objects of a blueprint that cannot be reused

  the objects of a blueprint that can be reused

 3. The process of creating multiple objects of (or based on) a class is called:

  mutation

  polymorphiation

  instantiation.

  encapsulation

 4. You could create a class called 'Person'. In this example, note that any given person has a name, and can 'do' things like "speak" and "walk". Speaking and walking are examples of ...

  class objects

  methods that would be contained/created inside the class

  attributes that would be contained/created inside the class

  object classes

 5. Here is a simple custom class which stores information about a person. Name two attributes that have been defined in this class.
import datetime # we will use this for date objects

class Person:

    def __init__(self, name, surname, birthdate, address, telephone, email):
        self.name = name
        self.surname = surname
        self.birthdate = birthdate

        self.address = address
        self.telephone = telephone
        self.email = email

    def age(self):
        today = datetime.date.today()
        age = today.year - self.birthdate.year

        if today < datetime.date(today.year, self.birthdate.month, self.birthdate.day):
            age -= 1

        return age

person = Person(
    "Jane",
    "Doe",
    datetime.date(1992, 3, 12), # year, month, day
    "No. 12 Short Street, Greenville",
    "555 456 0987",
    "[email protected]"
)

print(person.name)
print(person.email)
print(person.age())

  Jane,datetime

  person, class

  age,date

  name, surname

 6. Inside the class body, we define two functions - these are our object's methods. The first is called __init__, which is a special method. The purpose of this method is:
import datetime # we will use this for date objects

class Person:

    def __init__(self, name, surname, birthdate, address, telephone, email):
        self.name = name
        self.surname = surname
        self.birthdate = birthdate

        self.address = address
        self.telephone = telephone
        self.email = email

    def age(self):
        today = datetime.date.today()
        age = today.year - self.birthdate.year

        if today < datetime.date(today.year, self.birthdate.month, self.birthdate.day):
            age -= 1

        return age

  to initiate all the attributes and methods by calling them at once

  to set up a new object using data that we have provided.

  to remove any existing data from the set attributes

  to initialise the variables by ensuring they are hidden

 7. What is the age method (starting on line 14) doing?
import datetime # we will use this for date objects

class Person:

    def __init__(self, name, surname, birthdate, address, telephone, email):
        self.name = name
        self.surname = surname
        self.birthdate = birthdate

        self.address = address
        self.telephone = telephone
        self.email = email

    def age(self):
        today = datetime.date.today()
        age = today.year - self.birthdate.year

        if today < datetime.date(today.year, self.birthdate.month, self.birthdate.day):
            age -= 1

        return age

  creates a template in which birthdate and currentdate are stored

  creates a blueprint with attributes that set out the pseudocode to solve the problem

  calculates the age of our person using their birthdate and current date

  creates a function that calls itself.

 8. You may have noticed that both method definitions have "self" as the first parameter, and we use this variable inside the method bodies but we don't appear to pass this parameter in (e.g on Line 35) Why?
import datetime # we will use this for date objects

class Person:

    def __init__(self, name, surname, birthdate, address, telephone, email):
        self.name = name
        self.surname = surname
        self.birthdate = birthdate

        self.address = address
        self.telephone = telephone
        self.email = email

    def age(self):
        today = datetime.date.today()
        age = today.year - self.birthdate.year

        if today < datetime.date(today.year, self.birthdate.month, 
								 self.birthdate.day):
            age -= 1

        return age

person = Person(
    "Joe",
    "Bloggs",
    datetime.date(1992, 3, 12), # year, month, day
    "No. 10 Python Street",
    "555 456 0987",
    "[email protected]"
)

print(person.name)
print(person.email)
print(person.age())

  That is incorrect, Line 35 should read: print(person.age(self))

  because self refers to python itself, and python knows!

  because whenever we call a method on an object, the object itself is automatically passed in as the first parameter

  because 'self' is a variable that has been set to 'self' in the__init__ method

 9. What is happening on lines 7 and 8?
class Person:
    pets = []

    def add_pet(self, pet):
        self.pets.append(pet)

jonathan = Person()
ruth = Person()

jane.add_pet("cat")
print(jane.pets)
print(bob.pets) # oops!

  We are encapsulating the values jonathan and ruth inside the class Person

  We are creating two instances of the class Person; jonathan and ruth

  We are information hiding the class person attributes from 'jonathan' and 'ruth'

  We are creating two variables jonathan and ruth that contain the string 'Person()'

 10. Line 11 is creating a new instance of the class Person. Is there an error?
class Person:
    def __init__(self, height):
        self.height = height

    def get_height(self):
        return self.height

    def set_height(self, height):
        self.height = height

joe = Person() 

  Yes. It should be: joe = Person(153) #pass in the height parameter

  No. It will successfully create the object 'joe'

  Yes. It should be joe = Person(self,153) # pass in parameters self and height

  Yes. It should be joe=Person(height) #pass in the height parameter for reference