What Are Objects And Classes?2:45 with Kenneth Love
There are a lot of pieces to Python's object-oriented programming features. Let's talk about them so we can have a shared vocabulary for the rest of the course.
class keyword lets us define a class. We use it just like we do the
def keyword for making functions. Class definitions are blocks like function definitions.
Inside of the class, variables are called attributes.
class NewClass: name_attribute = "Kenneth"
And functions that are defined inside of a class are called methods.
class NewClass: name_attribute = "Kenneth" def name_method(self): return self.name_attribute
Whenever we call a class, it creates an instance. Each instance has full access to the all of the attributes and methods of the class.
new_instance = NewClass() new_instance.name_method()
[SOUND] [SOUND] Hey everyone. 0:00 I'm Kenneth, the Python teacher here at Treehouse. 0:05 In this course I'm gonna introduce you to one of the most powerful 0:07 features of Python. 0:10 But like many things that give you great power, 0:11 this one will give you great, no wait, this isn't some comic book. 0:13 This feature will give you a lot more power and capabilities in you programming. 0:16 But it won't suddenly make you responsible for the safety of a major city. 0:20 In the time you spent with Python you probably read or 0:23 heard that Python is an object oriented programming language. 0:25 But like so much in the world of programming, 0:28 that very descriptive name, object oriented programming language, 0:30 doesn't really mean all that much, does it? 0:33 Yet, more dense programming jargon that we just throw at people. 0:35 All right, let me break it down a little bit for you. 0:37 When I was in school and 0:40 we talked about grammar, nouns were always defined as a person, place, or a thing. 0:41 And on the surface level, 0:45 that's a pretty good explanation of an object in programming. 0:46 If it can be described with adjectives and 0:49 maybe it can do things with verbs, it's an object. 0:51 In Python though, everything is an object. 0:54 When you've used an integer to represent someone's age, you used an int object. 0:56 When you stored your name in a string, you put your name into an str object. 1:00 And heck, when you created a function, you actually defined a function object. 1:04 Most of the time though, we don't need to think about functions being objects. 1:07 Now, before I get into some terms and definitions, let me say this. 1:11 Object oriented programming is a major area of programming and program design. 1:15 You won't understand all of it after doing just this course. 1:19 I've been using the school of design for over a decade and 1:22 I keep learning new patterns and approaches to it all the time. 1:24 So, if at the end of this course you still feel a little fuzzy about it, that's fine. 1:27 It's to be expected. 1:30 Just like with any large topic area, like say algebra or baking, you'll have to 1:32 spend time researching and experimenting with it to really get the most out of it. 1:36 Okay, let's define a few things and then we'll get into some coding. 1:40 In Python, object types like int and str and 1:43 list are defined with a construct known as a class. 1:46 Instead of using def like in functions, we use the key word class. 1:49 Nouns have adjectives to describe them. 1:53 Objects, or classes, have attributes. 1:55 Attributes are very similar to variables but 1:58 they're defined inside of a class and belong to that class. 1:59 And just like nouns can do actions by using verbs, 2:03 classes have methods to give them special abilities. 2:06 Methods look just like functions, but again, they belong to the class. 2:08 When we use a class, 2:12 like creating a string from a number, we call those resulting object an instance. 2:14 When that instance is used, like when we change an attribute on it or call 2:18 a method, the instance is responsible, not the class it was instantiated from. 2:21 There are actually some other things to consider about attributes and methods, but 2:26 we’ll dive into them later. 2:29 Wow, that was a lot to take in and we haven’t even gotten into code yet. 2:30 You’ll get used to all of this new vocabulary quickly though, 2:33 because we’ll be using it all through this course. 2:36 For now, get settled in and read through the teacher’s notes to make sure you’re 2:38 comfortable with all these new words. 2:41 There’s a bunch of code and a vocabulary test coming your way. 2:42
You need to sign up for Treehouse in order to download course files.Sign up