Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Object-Oriented Python Advanced Objects Constructicons

Juneau Lim
Juneau Lim
13,362 Points

Still confused with @classmethod

I read all the questions here, but it's still unclear for me.

  1. is @classmethod more like an annotation, or it actually functioning? My best guess is, we can only use cls when we are using @classmethod, and when we use it, it works as if we call it outside class. so, therefore, just as if we did Bookcase(book_list).

  2. at the note,

def create_bookcase(self, book_list):
    for author, title in book_list:
        self.append(Book(author, title))
    return self

why are they attending to self, not to self.books? Bookcase class never inherited list, but how can it use .append()?

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

Good question! You asked is @classmethod more like an annotation, or it actually functioning?

The decorator @classmethod is more than an annotation, it marks the method as belonging to the class and makes the method assessable from the class. A classmethod does not require a class instance to run the method.

In the same way the self of a "regular" instance method is assigned to point at the current class instance (done at instance creation), in a class method the first parameter cls is assigned to point to the class itself. This makes it straightforward to reference the class from within the class method.

My best guess is, we can only use cls when we are using @classmethod, and when we use it, it works as if we call it outside class.

Correct. The class method would be referenced using ClassName.method_name(). Notice there are no parens after the ClassName. If there were parens, that would cause an instance of the class to be created. Not trying to make things more confusing, a class instance my also access class methods from the instance though it is not the usual usage model for class methods.

*So, therefore, just as if we did Bookcase(book_list). *

While it may be possible to have a class method that behaves the same as the __init__ method, in this situation, the Bookcase.__init__ method is looking for a list of Book instances, and the Bookcase.create_bookcase() classmethod is looking for a list of author, title pairs that it will use to create Book instances, and then append those to the books attribute.

why are they attending to self, not to self.books? Bookcase class never inherited list, but how can it use .append()?

You are correct! Bookcase itself does not have an append() method define. As is, the code in the notes would not work. Though it could with the additional following method:

    def append(self, item):
        self.books.append(item)

Post back if you wish more help. Good luck!!

Juneau Lim
Juneau Lim
13,362 Points

Wow, Thanks for the detailed answer! It is very helpful.