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.

Adding Attributes to a Python Class

Give Feedback

In this video, we start building our Dog class and fill it out with attributes.

We can instantiate our Dog class like this

philo = Dog("Philo", 5)

We can access the instance attributes with dot notation.

print(philo.name)

Comments & Discussion

malcolmgandrews on Sept. 30, 2019

I don’t want to be a pedant but a Mouse is a Mammal.

richardojeda1985 on Oct. 24, 2019

ok what if you are working in a class with user input? how would you call the str method??

Austin Cepalia RP Team on Oct. 27, 2019

I’m not exactly sure what you mean by “working in a class with user input.” There are a few approaches you can take if you need user input. The first one is to instantiate the class with the input already stored in a variable like this:

class Person:
    def __init__(self, name):
        self.name = name

name = input('enter your name: ')
person = Person(name)

Or you could ask for user input in the __init__ method like this:

class Person:
    def __init__(self):
        self.name = input('enter your name: ')

person = Person()

str() is used to convert a value into a string. input() returns a string, so I’m not sure why you would need to call that. If you’re talking about getting a string representation of the object instantiated from your class, you can do something like this:

class Person:
    def __init__(self, name):
        self.name = name

    def __str__(self):
        return 'this object has name ' + self.name

name = input('enter your name: ')
person = Person(name)
print(person)

This is beyond the scope of this course, but str defines the string representation of an object (This is similar to overriding ToString() in other languages). When we pass the person object to the print() function, it calls str under-the-hood and prints the string representation of the object. You can learn more about that here Let me know if you have any other questions!

kwf777 on Nov. 2, 2019

Why do you use {} and format instead of just printing the attributes?

Austin Cepalia RP Team on Nov. 5, 2019

@kwf777 This was my first course. I was under the impression I needed to follow the article exactly, which used .format() because it’s a bit older. I use fstrings in all my courses now :)

Oleksii Potapenko on Nov. 13, 2019

At 5:35 output is changing without any run code.

sideproject8 on Dec. 17, 2019

Please re-record this with the new f-strings. Might be silly to ask but as someone who is really trying to learn python, this small issue just keeps messing me up and takes me out of the flow of your courses, when other videos on here use f-strings. Please and thank you.

mnemonic6502 on Jan. 18, 2020

Regarding species not being printed second time around after species was changed to ‘mouse’ was useful to highlight, but took reviewing. Could have been made clearer by saying something like it’s not printed out on right because yada yada etc (rather than assuming the learner knew the lack of output as a parallel from your technical explanation).

Lokman on Feb. 27, 2020

Hi Austin,for clarification that class attributes is a permenant attributes compare instance attributes that can modify even outside of class code blocks.

Ricardo on April 11, 2020

This video was awesome!

Zarata on April 14, 2020

A rose by any other name smells as sweet … I’m a Java person, but not hugely “formally” trained. Thus, if the high keepers of the Java flame make a distinction between class attributes and instance attributes, (properties), I don’t remember. I simply define class vars in Java and give them default values, or leave them null and give them values during construction – the practical results always seem the same. (I’m bypassing visibility, protection, etc.) Here in Python the class and instance var terminology functionally doesn’t seem to make a lot of difference either, though I do see the conceptual difference. What I’m trying to express is that as soon as you say

__init__(self,myinstanceattr)
    self.myinstanceattr

then “myinstanceattr” becomes an inextricable part of every self example of the class just as a class attribute would be – the only difference is that “classattr” would have a pre-assigned default value (which I find can even be None). However, the instance attributes are “required” to be assigned values during the creation of the class instance (haven’t seen an automatic default init yet, though can define an equivalent). Once you have an instance, both the class and instance attributes can be accessed and modified using the “.” operator. So, long short: I’ll be waiting to see in the next vids if there’s any deep down difference behind “class” vs. “instance” vars, like speed of access or storage locations or something.

Very good presentations. Clean, well thought, great delivery. Thespian? Experienced teacher? Little things make me suspect you’re a C++ or Java guy also, either now or in your beginnings.

Austin Cepalia RP Team on April 14, 2020

@Zarata Not thespian, but I do have a lot of experience teaching. Particularly with kids who don’t respond well to boring teachers. You were close with the languages too, my background is primarily C# and .NET :)

Cory on May 1, 2020

So in essence everything in python we’ve been learning is oop? Is that why certain built in functions work with str.object and not with tuple.object?

Become a Member to join the conversation.