What Are Objects And Classes?

If you notice a directory named __pycache__ in your Workspace, don't worry about it. Python 3 creates this directory to store compiled bytecode in. This makes your programs execute faster but isn't something you need to bother with at all.

0:00

[MUSIC]

0:04

>> When we talk about doing object
oriented programming,

0:07

we're talking about using bundles of code
that we call objects or classes.

0:12

Classes let us create predictable objects
that we can work with and

0:15

that line up to a mental model we already
have.

0:19

Everything in Python is an object but

0:20

that's a bit further down the rabbit hole
than we're gonna go in this course.

0:24

You should remember it, though.

0:25

It might be important on a quiz later.

0:28

As an example of custom classes maybe we
know that in the game we're gonna

0:31

build throughout this course we'll always
make monsters have certain attributes.

0:36

We know that they'll have a number of hit
points, a color, [NOISE] and a weapon.

0:41

Since we know our monster will always have
these attributes, making an object that

0:46

represents a generic monster gives us a
great way to organize our code and

0:49

our thoughts.

0:51

Let's go make a monster class now.

0:54

Usually we only have one class per file,
so

0:56

let's make a monster.py that will hold our
monster class.

1:01

So, we'll name it monster.py and inside
here is where we're gonna write our code.

1:08

So, just like when we create functions and
we have to use the def keyword,

1:11

we have to use the class keyword when we
make a class.

1:15

Classes also usually start with a capital
letter, so

1:17

we'll name this one Monster with a capital
M.

1:23

And we can specify the inheritance chain,
or

1:26

the other classes that our class modifies
of our class if we want to.

1:30

But they're not required for basic classes
in Python 3.

1:33

That's what this parenthesis object
parenthesis is.

1:37

Since they're not required let's just
leave it off for now.

1:40

Inheritance trains are a program
equivalent to your family tree.

1:44

Classes though, are blocks, just like
functions, so

1:47

everything has to be tabbed in.

1:50

You start to develop a habit in Python of
always tabbing after a colon ends a line.

1:54

This helps you read your code quickly and
know these attributes and

1:57

methods go with this class.

1:59

Okay, let's give our monster some
attributes.

2:02

Attributes are just values that the class
holds onto, we can override them on

2:06

instances of the class, but we'll talk
about that in the next video.

2:09

Let's give our monster hit points.

2:11

[SOUND] A color.

2:15

[SOUND] And a weapon.

2:21

Great.

2:22

So by default, our monster is yellow,
carries a sword and has one hit point.

2:27

We can check these things in the shell.

2:29

So, let's pop down here to our console and
go into Python.

2:36

So, we'll just do Python.

2:37

We're not gonna run a script, we're just
gonna go into Python.

2:40

And then we will do from monster import
monster.

2:46

Now this is slightly new import syntax.

2:49

What it just says though is that it wants
to bring something from inside of

2:53

this library that we named.

2:54

In this case, we just want the monster
class that's inside of

2:58

the monster library that we created.

3:01

And yes, every file that you create is a
library.

3:04

So, let's check those attributes.

3:06

Monster.hit point, one.

3:10

Monster.color, yellow.

3:14

And Monster.weapon is sword.

3:18

So, great, those are exactly what we
expected.

3:21

I'm sure you noticed, but when we're using
attributes we don't have to

3:24

include the parenthesis like we do for
functions.

3:27

That's because attributes aren't functions
so we don't have to call them.