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

compare and contrast - songs.py - def __int__ confusion

The code checker accepts the solution below.

class Song:
    def __init__(self, artist, title, length):
        self.artist = artist
        self.title = title
        self.length = length

    def __int__(self):
        return self.length

    def __eq__(self, other):
        return int(self) == other

First, wouldn't one expect def int to be something like:

    def __int__(self):
        return int(self.length)

An online community support comment indicated the above would result in an infinite loop. Is this true? If so, wouldn't one expect something like (this fails the code checker, btw):

    def __int__(self):
        return super().__int__(length)

1 Answer

self.length isn't of type Song, so I don't see how it can cause an infinite loop.

In the case of super().__int__(length), super() is the class object (the default class all classes inherit from), and it does not have any generic __int__ method that applies to all sub-classes.

"self.length isn't of type Song". Of course! Thanks, that helps cement the concept.

As to def int(self): The code checker seems to accept either def int(self): return self.length or def int(self): return int(self.length)