Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 7: Wizard Battle App
Lecture: Concept: Objects vs. Classes
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Let's take a moment and focus on this concept of classes versus objects. If you are new to object oriented programming and this idea of classes,
0:09
this can be a little bit tricky. Recall, this is how we define a class, we say class creature and it defines basically the blueprint.
0:18
Here is how we initialize it, and within that initialization here is the fields, here is the other behaviors it has for example that it can walk around
0:26
and for creatures, there is only one creature class, this is it, this is what a creature looks like in our program.
0:34
However, we can create many of these creatures, here we are going to create a squirrel and its power,
0:41
you can see the parameter to the initialization method is power so here we want to say creature (7),
0:47
we'recreating a squirrel with power 7 whatever that means and then we are also creating a dragon which is more powerful, it has power 50,
0:54
so when we create the squirrel and the dragon, these are objects, and objects are created via classes but they each have their own pointer in memory
1:04
and that points to their own data. So, this squirrel object it has its own power variable and its power is 7.
1:11
Now it actually gets the implementation from walk, from the blueprint, but we can call walk on this squirrel and we'll work with its data,
1:20
its power of 7 and so on. The dragon has another pointer, somewhere else in the memory and it has its own copy of power,
1:27
so if we change the dragon's power obviously it's unrelated to the creature so these are objects,
1:32
they are created from the class, from the blueprint of what defines a creature, and then we can call squirrel.walk(), dragon.walk()
1:40
and they may have different behavior based on the power, maybe if there is a lot of power it jumps around
1:45
if it doesn't have much it sort of scurries or something like this. Once you get used to this idea of classes and objects
1:51
it's really straightforward and you'll have it down no problem, but if this is new to you, make sure that you get this idea,
1:56
it's the core concept in object oriented programming classes, inheritance in object oriented programming play a really central role in Python.