Eric Brauer
instructions
if condition
loop
more instructions
...
Basically, start on the first line and go to the end. No way to reduce complexity, to reduce redundant code, or allow the code to be reused in any way
def calculate_area(length, width):
...
# instructions
x = calculate_area(l1, w1)
y = calculate_area(l2, w2)
...
We can organize blocks of code that perform a specific task into functions. This is especially useful when we will repeat certain tasks many times. However, the function has to be defined in the same place as where it’s called.
Additionally, we can put our functions into modules, and import these modules into other programs. There still exists a limitation, though: data in our functions is not persistent. Every time we use calculate_area, we are sending it new values to work with.
Objects are different in that they contain both attributes and methods.
Consider a shape like a rectangle: it has attributes like length, width, colour.
It also can be interacted with: it can be moved, it can be changed, etc.
Put more concretely, an object can contain data and functions (methods).
We’ve already been working with objects in this course, and you’ve probably already experienced the advantages of Object-Oriented Programming!
The dot (.
) is a way of indicating how things are
organized. It tells us that a thing belongs to another
thing.
You might see it used when working with imported modules:
Or it might be used to indicate that the thing is a method/attribute belonging to an object.
A class is a way to define a template for one or more objects we wish to create.
Once you define a class, you can create many objects from that class.
__init__
Our Rectangle
class would be more useful if we could
start with different values for length and width. Fortunately, we can
using a special method called __init__
.
The self
keyword has to be the first
parameter of each object method, and is in front of each
object attribute.
It identifies things as belonging to the object that it is created with.
Parameters in methods behave exactly like parameters in functions, they are forgotten once the method ends.
Therefore, in order for attributes to be remembered outside
of __init__
is to save them into self
.
Consider a ‘Student’: students have names, student numbers, addresses, payment info, courses and transcripts.
Students can be enrolled in courses, can drop courses, have a GPA, make payments, graduate, etc.
In the previous example, we can see two instances of the Student class.
student1 = Student('joe', '3124')
is one of them.
student1
is an object, so is student2
.
Each object will have their own attributes.Student
is a class, which defines methods and
attributes for each object.Objects are mutable and persistent. Meaning: you can edit the data inside objects, and those changes will be remembered until the object is destroyed.
class Student: # classes get upper case letters!
def __init__(self, name, number): # init is used to define initial values.
self.name = name # data attribute is given the value of the parameter
self.number = number
self.courses = {} # we create an empty dict
self
is a required parameter for each method
inside the class.Let’s assume that I typed those slides into a file called
student_mod.py
. We can import it in our
Python interpreter.
Data attributes are for maintaining an object’s state. As we’ve said, data attributes are persistent and mutable
Methods are functions that ‘belong’ to the object. They must have ‘self’ as a parameter