0% found this document useful (0 votes)
53 views15 pages

TEC102 Week 11 Workshop

Here is an Animal class with a constructor that initializes name, species, and age instance variables: class Animal: def __init__(self, name, species, age): self.name = name self.species = species self.age = age

Uploaded by

aiexplorer009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views15 pages

TEC102 Week 11 Workshop

Here is an Animal class with a constructor that initializes name, species, and age instance variables: class Animal: def __init__(self, name, species, age): self.name = name self.species = species self.age = age

Uploaded by

aiexplorer009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

TEC102

Fundamentals of
Programming
Lesson 11
Classes
COMMONWEALTH OF AUSTRALIA
Copyright Regulations 1969

WARNING

This material has been reproduced and communicated to you by or on behalf of


Kaplan Business School pursuant to Part VB of the Copyright Act 1968 (the Act).

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.

Do not remove this notice.


Subject Learning Outcomes
1 Interpret simple program
specifications.
2 Conceptualise a high-level model via the
use of pseudocode and flow charts.
3 Describe how to transfer a high-level
model into a software application via the
use of a programming language.
4 Use an integrated development
environment to develop, debug and
test a solution written in a
programming language.
5 Use a programming language to read and
write data to a persistent storage.
Recap: Types
• Recall, from Workshop 1 – Hello World!,
the built-in Python type() function can be
used to return the type of the specified
object: a = [1, 2, 3]
b = ':D'
c = 33
d = 3.0
print(type(a)) # <class 'list'>
print(type(b)) # <class 'str'>
print(type(c)) # <class 'int'>
print(type(d)) # <class 'float'>
Class
• Another type of object that was mentioned, but not discussed is
class
• A Class is like an object constructor (or a way to create an object)
• Use the class keyword to define a Class
– Ensure proper PEP 8 Style Guide: https://peps.python.org/pep-0008/
• In the example, a class was created and named Test
– The pass keyword was used on purpose to not cause an
IndentationError
– The type output was: <class '__main__.Test'>

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

def how_many_pounds(self, kg):


return kg * self.kg_in_pounds

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

bob = Person('Bob', 'Cook')


ana = Person('Ana', 'Nurse')

print(ana.name) # prints Ana


print(ana.job) # prints Nurse
Instance Variables
• In the previous slide, the object ana, must provide
two arguments to the Person() class constructor
• When the two arguments “Ana” and “Nurse” are
passed, the Person class is instantiated with the
instance variables self.name and self.job to
represent those passed arguments
• Note: important to know that the object bob, can’t
access object ana instance variables
• To check what instance variables are mapped to
what object, use the __dict__:
print(ana.__dict__)
# {'name': 'Ana', 'job': 'Nurse'}
Activity
Create an Animal class that has a name,
species, and an age

Answer:
class Animal:
def __init__(self, name, species, age):
self.name = name
self.species = species
self.age = age

You might also like