So, it's
Monty Python Flying Circus
okay actually it is not but just so you know
this is where the python language got its name
it is from the comedy
but today's talk will be about OOP or object-orientated programming in python
And I am Junxi, I am a second-year student from Singapore University of Technology and Design
currently in the Computer Science / info systems major
And I am also currently an intern in Tinkerbox
In fact, it is my fellow intern and TechLadies' Kate who invited to speak here today
so I am glad to here
so now i understand that most of the audience here are beginner level
Just to get a gauge, how many of you guys have done OOP in python before?
Okay, I will say that for those who have not done anything
I hope that by the end of this talk
we will get to understand more about
what OOP is about
'Kay, I would say OOP is like an intermediate level topic
but it is a very key topic for you to understand
for you to move on to do more advanced stuff
so this is why I chose the topic for today
but also it is a very broad topic
so i don't know how much I can cover in 20 minutes
So forgive me if I speak too fast
Or skip over certain things
okay
so
just a brief idea of what we will be talking about
We will only be covering, like, nuts and bolts
of OOP in python
So language-specified construct syntax
That sort of thing
Because I believe for beginners
this will be what you will be more interested in
but at the same time, i hope i will be able to talk a little bit about
what you called Object-oriented design
principles of writing good codes
with design principles, basically
so
Before I actually go into the actual materials
I have a confession to make
The sad truth is, okay maybe not that sad
but the truth is that I probably enjoy drawing silly things
more than I like programming so
as a result, you will see a lot of
silly drawings
Probably more silly drawings than code
in this talk
but it is my hope that you know
it will sort of give you a visual aid
to understanding the concepts
that I will be talking about
Okay so
let's dive right in
Nuts and bolts
so these are the topics I will be covering
Objects Classes & Instances
Attributes and Methods
Inheritance and Scope
And those words have completely
flown over your head
Do not fear
by the end of this talk
you should, hopefully,
have a rough idea of what it means
so
let's jump straight into the first...
oh wait, sorry. before that
i just want to make it clear that
in common use right now
there are two main python versions
so python 2 and python 3
there are some differences in these 2 versions
so as beginners
it can be easy to trip up
over these differences
so just to be clear that
when you read up resources online
for example, you should know
what the version
those resources are talking about
which python version they are talking about
so for clarification
just be clear
I am using python 3 for all my examples here
alright
so objects
think of objects as a way of
logically encapsulating your code
so objects has what we called attributes
and methods
so let's look at this doggeh over here
i like to think of attributes as
kinda like
adjectives?
or properties
So for example you say
the dog is white
or can have a weight of 7 kg
so these are like the attributes of the dog
on the other hand
methods
they are kinda more like verbs
a dog can wag tail and bark
so these are the things
the dog can do
these are like
methods of my dog object
this is a slightly lame description
but i hope can bring across
into the field of what
I mean by attributes and method
and in fact, you know
in python
you have your variables and your functions
right?
so your attributes and methods
are just your good ol' friends
your variables and functions
the only thing is that
attributes are kind of like variables
that are associated with a certain object
and methods are functions
that are associated with a certain object
in the end,
objects basically is a way of calculating data
and a form of attributes
with functions of procedures
in the form of methods
Okay
so let's look at this dog transcribe into
real python code
for those who have not done any OOP in python before
this syntax might look intimidating but
hopefully by the end of it
you will be able to understand
what's going on
The kind of object-oriented programming
in python
that we are dealing with
is called class-based object oriented
programming
This is the kind of OOP
you will find
in languages like Ruby, Java, C# and etc.
Only the most popular kind of OOP
Just so you know,
there are other kinds out there
Class-based OOP
you see here that
I am defining this class "dog"
you should know by now
that in python,
indentation is intactly(?) significant
so all these indented code
this is inside the class body.
This is my class definition.
Now I am talking about classes
Now I have to talk about
like explain about
classes and instances
Because we are talking about class-based OOP
so what are classes and instances?
So, think of a class as a kind of category
where an instance is
particular living example
of a kind of thing
To drive the point across,
let me talk about babies
Yes! Babies
When you think of
a baby right..
a human baby
you kind of have this conception
tectonic idea of what babies
should look like
sweet, tender, angel
angelic things
That is sort of like class
this general category
this general idea
whereas if you actually
see this real living kicking screaming
pooping baby
you are like
"What's this ugly yoda-like thing?"
That is kind of like instance
that's the real concrete
instance of a baby
So I hope that kinda brings across the idea
of what's the differences between
the class and instance
Let's go back to the code
and in my class
so in this class definition right
when I define my class
you can think of it as
defining a custom data type
so you know in python
we have our built-in types
we have integers
we have strings
we have lists
we have dictionaries
When you define your class
it is as if you are building
your own data type
And this one data type
has its own attributes and
its own methods
I will go through the stuff here later
but let me go down to the code here
so what you see down here
in this line, line 13
this is where I instantiate
I create an instance for my class
So I am assigning
I am creating
an instance called "Doggeh"
And this the way I construct
my instance
by calling the, what we called
the class constructor
So this is the class name
the parenthesis
I pass in certain arguements
to initialise my instance
so now I have this Doggeh
which is an instance of a dog class
Okay
so now that we sort of know
what classes and instances are
let me go back
I will start going through the code here
So to understand the code in here
we have to talk about
attributes and methods
When we talk about attributes and methods
There are two kinds
They can be instance attributes or instance methods
or they can be class attributes and
class methods
But for now
I will talking about instance attributes
and instance methods
Because that is what we usually deal with
Inside this class body
I define several methods
for my dog class
so these are how you define functions
I mean, methods are just functions
but they are inside a class
This is how I define my methods
And
you notice that
this first method looks kind of special
with the double underscore in front
So double underscore init double underscore
so i will be talking more about this later
but let's look at these two
func.. methods first
Now you will notice something interesting
about this method
I mean, in the end,
these methods are just stuff that
they print something to the console
right
It is going to print "Wag Wag" or
"Woof Woof"
but there is something interesting about this
in the method signature
I am actually taking this parameter 'self'
and
this 'self' is actually an indication
to you that
these methods are instance methods
We can invoke these instance methods
by using this Dog syntax over here
so I invoke "wag tail" by
calling it Doggeh.wag_tail()
And just to emphasize that you have
to put the parenthesis there
to invoke the method