In this video, you’ll learn where classes and objects are used in real software, as well as how they’re defined in Python.
We can define an empty Dog class like this:
class Dog: pass
Classes contain characteristics called Attributes. We make a distinction between instance attributes and class attributes.
Instance Attributes are unique to each object, (an instance is another name for an object). Here, any
Dog object we create will be able to store its name and age. We can change either attribute of either dog, without affecting any other dog objects we’ve created.
class Dog: def __init__(self, name, age): self.name = name self.age = age
__init__ is called the initializer. It is automatically called when we instantiate the class. It’s job is to make sure the class has any attributes it needs. It’s sometimes also used to make sure that the object is in a valid state when it’s instantiated, like making sure the user didn’t enter a negative age for the dog.
We have to include the
self parameter so that our initializer has a reference to the new object being instantiated.
Class Attributes are unique to each class. Each instance of the class will have this attribute. It’s sometimes used to specify a defualt value that all objects should have after they’ve been instantiated. Here, our class attribute is species
class Dog: species = 'mammal' def __init__(self, name, age): self.name = name self.age = age
Peter T on March 22, 2019
In the text below the video, don’t you need to change: “Behaviors are actually called Attributes.” to “Properties are actually called Attributes & Behaviors are actually called Methods.”?