Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Class and Instance Attributes

Give Feedback

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:

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): = name
        self.age = age

This __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): = name
        self.age = age

Comments & Discussion

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.”?

Dan Bader RP Team on March 22, 2019

@Peter: Good catch, this should be fixed in the description text now. Thanks :)

mnemonic6502 on Jan. 18, 2020

At this point it would have been better if the learner wasn’t expected to mentally context switch thinking from the hypothetical door class then to a dog class and just stuck to one example class initially and built on that.

Become a Member to join the conversation.