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