It's easy to create classes in Python. In Python, a class is simply a container for data and functions. This lesson describes how to create a class in Python.
- [Instructor] A class is the basis of all data in Python, everything is an object in Python, and a class is how an object is defined. Here in Komodo, I've opened a working copy of class.py, from chapter nine of the exercises files, and down here, beginning on line four we see our class definition beginning with the keyword class, and the name of the class, and a colon, and then everything that defines the class is indented under that declaration.
We have data in the form of these variables, and we have methods in the form of these function definitions. You'll notice that the first parameter of a method is always self. Self is not a keyword. You can actually name that first parameter whatever you want to, but self is traditional and I highly recommend that you use self so that as people are reading your code, they know what you're talking about. So the first argument is self, which is a reference to the object, not the class, to the object.
And so, when an object is created from the class, self will reference that object. And then everything that references anything defined in the class is dereferenced off itself to get the instantiated object version of it. And the period operator is used to dereference the object. And the same is true outside of the class. So here we've defined a variable donald from the class Duck, and then in line 16, we invoke the object method quack on the object donald.
So donald is the object, because it was instantiated from the class, the dot operator dereferences the object so that you can get to the method in this case quack. And so quack and move, as you can see, they print the sound and movement variables from up here, and so when I save this and run it we get those two strings printed. In fact, if you wanted to, although it's not recommended, you could simply print donald.sound, a variable itself, and get exactly the same result.
So this is the basics of how a class is defined, we'll cover many more details about classes and objects in the rest of this chapter.
- Python anatomy
- Types and values
- Conditionals and operators
- Building loops
- Defining functions
- Python data structures: lists, tuples, sets, and more
- Creating classes
- Handling exceptions
- Working with strings
- File input/output (I/O)
- Creating modules
- Integrating a database with Python db-api
Skill Level Intermediate
Python: Programming Efficientlywith Michele Vallisneri2h 15m Intermediate
Learning Python Web Penetration Testingwith Christian Martorella2h 49m Intermediate
2. Language Overview
3. Types and Values
8. Structured Data
11. String Objects
12. File I/O
13. Built-in Functions
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.