Objects
Explore objects and classes in Python.
We'll cover the following...
Introduction to objects
We are going to look at one more Python idea called objects. Objects are similar to reusable functions because we define them once and use them many times. But objects can do a lot more than simple functions can.
The easiest way to understand objects is to see them in action rather than talk about them as an abstract concept. Let’s have a look at the following code:
# class for a dog objectclass Dog:# dogs can bark()def bark(self):print('woof!')passpass
Let’s start with what we’re familiar with. We can see there is a function called bark()
inside the code. It’s not difficult to see that if we called the function, we would get "woof!"
printed out. That’s easy enough.
Let’s look at that familiar function definition now. We can see the class
keyword, and a name Dog
, and a structure that looks like a function. We can see the parallels with function definitions that also ...