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

How is this question being run to test? I'm confused if by instance or by running the method.

I've been pretty confused throughout this course, and I believe it's because the code challenges and Kenneth's examples aren't correlating in how regular methods, class methods, and static methods are being instantiated and or run/called.

For example, based on the code listed, when I run the class method in the console "Letter.from_string('dot-dash-dot-dash')" I get the desired output but it does not pass.

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, str_lis):
        cls.str_lis = str_lis
        new_lis = []
        lis = str_lis.split("-")
        for i in lis:
            if i == "dash":
                new_lis.append("_")
            if i == "dot":
                new_lis.append(".")      
        return new_lis


class S(Letter):
    def __init__(self):
         pattern = ['.', '.', '.']
         super().__init__(pattern)
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)


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

1 Answer

Steven Parker
Steven Parker
231,269 Points

You're really close! This code creates a correct pattern, but the challenge wants a new instance with the correct pattern in it. The "cls" parameter of a class method comes in handy for this:

        return cls(new_lis)

Steven Parker, thank you for chiming in, this allows my code to pass. I'm still confused if this class method can be instantiated in a way that prints the desired outcome in the console.

prior to adding your code I could run the below:

>>> a = "dot-dash-dot-dash"
>>> Letter.from_string(a)
['.', '_', '.', '_']

adding your code

<__main__.Letter object at .......>

Is there a way to produce the list rather than the instance in memory without adding a "repr or str" nested inside the Classmethod?

Steven Parker
Steven Parker
231,269 Points

Just access the pattern property on the returned instance:

>>> Letter.from_string(a).pattern