Move from Excel to Python with Pandas Transcripts
Chapter: Appendix: Python language concepts
Lecture: Concept: Classes

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Classes and object-oriented programming are very important parts of modern programming languages and in Python, they play a key role.
0:09 Here we are creating a class that we can use in some kind of game or something that works with creatures.
0:16 So to create a creature class, you start with the keyword class, and then you name the type and you say colon and everything indented into that block
0:24 or that code suite to do with the class is a member of the class. Most classes need some kind of initialization to get them started,
0:31 that's why you create a class, we want them to start up all ready to go and bundled up with their data and then combine that with their methods,
0:39 their behaviors and they make powerful building blocks in programming. So most classes will have an initializer,
0:46 and the initializer is where you create the variables and validate
0:49 that the class is getting setup in correct way, for example making sure the name is not empty,
0:53 the level is greater than zero, but less than a 100, something like that. Now this is often refered to as __init__ sometimes just init,
1:02 or even a constructor and these dunder methods because they have double underscores at the beginning and at the end,
1:08 they are part of the Python data model which lets us control many things about classes,
1:11 so you'll see a lot of methods like this but the __init__ method is probably the most common on classes.
1:17 If you want to create behaviors with your class, and if you have a class that's almost certainly part of what you are going to do,
1:23 you are going to define methods just like functions that are standalone, methods or functions that are parts of classes
1:30 and you define them in exactly the same way, the only difference is typically they take a self parameter,
1:36 the self parameter is passed explicitly everywhere when you are defining the class,
1:41 some languages have a "this" pointer, that's sort of implicit but in Python,
1:46 we call this self and it refers to the particular instance of the creature that exists,
1:51 you might have many creatures but the self is the one that you are working with currently. So just be aware you have to pass that explicitly everywhere
1:59 unless you have what is called a class method or a static method.

Talk Python's Mastodon Michael Kennedy's Mastodon