#100DaysOfWeb in Python Transcripts
Chapter: Appendix: Python language concepts
Lecture: Concept: objects vs. classes

Login or purchase this course to watch this video and the rest of the course contents.
0:01 When you are new to object-oriented programming, the idea of classes and objects often can seem interchangeable
0:08 and some people use them interchangeably; that's not really correct and so let's take just a moment and really clarify the relationship
0:16 and differences between classes and objects. So here we have a Creature class, you can it has an initializer and a walk method,
0:24 and notice that the walk method does something different if the creature is powerful, if its power is greater than 10 versus if it's lower.
0:31 This class is a blueprint for creating creatures. We could create a squirrel, we could create a dragon,
0:38 we could create a tiger, and those would all be specific objects or instances of the Creature class.
0:44 So down here we’re going to create a squirrel and a dragon, and notice the squirrel is created with power 7, the dragon is created with power 50.
0:51 Now these are both creatures, but they are now distinct things in memory.
0:56 Objects are created via classes and the squirrel object is somewhere in memory
1:01 and it has a power 7 and it has this walk behavior it gets from its class, but all of its variables are specific to it.
1:08 We have also the dragon creature, with its own variables, so it's power is 50 and if we change its power, it won't change the squirrel
1:14 or any other creature, just the dragon. And when we call squirrel.walk(), the squirrel is going to walk in some specific way based on its own power.
1:23 So you can see the Creature class test is a power greater than 10 or less than 10 and if it's greater than 10, it does something special,
1:30 maybe it walks in a powerful way versus a non-powerful way, who knows, but that will mean the squirrel walks in one way
1:36 and the dragon walks in another way, even though they are both instances of the Creature class.
1:41 So I hope that clears up the relationship between classes and objects.


Talk Python's Mastodon Michael Kennedy's Mastodon