TEC102 Week 11 Workshop
TEC102 Week 11 Workshop
Fundamentals of
Programming
Lesson 11
Classes
COMMONWEALTH OF AUSTRALIA
Copyright Regulations 1969
WARNING
The material in this communication may be subject to copyright under the Act. Any
further reproduction or communication of this material by you may be the subject of
copyright protection under the Act.
class Test:
pass
print(type(Test()))
Instantiation
• A class by itself doesn’t achieve anything useful
except being defined in code
• A class must be instantiated – which means we
must create an instance of the class, in order to
use it
• In the example, an empty class named Person
was created. Then, a variable bob instantiated
an empty Person() class constructor:
class Person:
pass
bob = Person()
Object-Oriented Programming
(OOP)
• A class instance is also referred as an object
• The pattern of defining classes and creating
objects to produce functionality
• If the Test class output type was:
<class '__main__.Test’>
– __main__ (two underscores __): refers to the Python
file that is executing the code
– In other words, the above output translate to: the
class Test was defined here, in the Python file that is
executing the code
Class Variables
• When the desired outcome is to have the same
data to be available to every instance of a class,
we use a class variable
• The class variable job is a variable that’s the
same for every instance of the class Person:
– We can access object’s (bob) class variables with the
object.variable syntax (bob.job)
class Person:
job = 'Cook'
bob = Person()
print(bob.job)
# prints Cook
Activity
In the previous slide, can you think of a reason
why creating a Person class for other objects –
which is most likely created to represent real
People – would lead to a bad outcome?
Answer:
Possibly, some of the other object’s created (or,
put simply other people) will not have the same
job as object bob did. Perhaps object ana might
be a Nurse, but the class variable defines
object ana as a Cook…
Methods (in a Class)
• Methods are functions that are defined as part of a class
– These methods belong only to that class
• The first argument in a method is always the object that
is calling the method
– Good practice: use self keyword as the first argument in a
method always
class Person:
job = 'Cook'
years = 15
def work_experience(self):
print('Experience: ' + str(self.years))
bob = Person()
print(bob.work_experience())
# prints Experience: 15
Methods with Arguments
(in a Class)
• Extending the previous slide, methods can take
other arguments and not just self:
• In the example, WeightConvert class was
defined, instantiated it, and used to convert
100kg to pounds:
class WeightConvert:
kg_in_pounds = 2.205
convert = WeightConvert()
print(convert.how_many_pounds(100))
# prints 220.5
Constructors
• There are several methods that can define a
Class to have special behaviour
– These are known as dunder methods (they are in
the form of two underscores on either side)
• An important dunder method in Class, is the
__init__() – which is used to initialise a
newly created object, and it’s called every
time the class is instantiated
• See next slide for example
Constructors (2)
• Revisiting our previous Activity, we can
attempt to fix the Person class, so that
each object can provide their arguments:
class Person:
def __init__(self, name, job):
self.name = name
self.job = job
Answer:
class Animal:
def __init__(self, name, species, age):
self.name = name
self.species = species
self.age = age