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

Using @classmethod

For some reasons I am getting RecursionErrors

morse.py
class Letter:
    def __init__(self, pattern=None):
        self.pattern = pattern

    def __iter__(self):
        yield from self.pattern

    def __str__(self):
        output = []
        for blip in self:
            if blip == '.':
                output.append('dot')
            else:
                output.append('dash')
        return '-'.join(output)

    @classmethod
    def from_string(cls, pattern):
        fix_me = Letter.from_string("dash-dot")
        return str(fix_me)


class S(Letter):
    def __init__(self):
         pattern = ['.', '.', '.']
         super().__init__(pattern)

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,426 Points

The recursion is coming from the line:

        fix_me = Letter.from_string("dash-dot")

Since it calls the method Letter.from_string from within the method Letter.from_string. This self reference causes a recursive call that loops until the recursion limit is reached.

Thanks Chris . I really appreciate all the help. I have an updated version of the code which I just posted with a for loop , but I keep getting try again error messages

Chris Freeman
Chris Freeman
Treehouse Moderator 68,426 Points

You can also look at other questions from the challenge to see their issues and fixes.