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

No method named `from_string`

please advice what i've done wrong

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)

    @classmethod

    def from_string(cls, string):
        corr = []
        new_string = []
        new_string =string.split("-")
        for i in new_string:
            if i == "dot":
                corr.append(".")
            else:
                corr.append("_")
        return corr

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,454 Points

You have the right idea, but are missing a key element of the task

  • The method from_string should be a classmethod of Letter, not S
  • The method should return a new instance with the correct pattern (['_', '.']).
  • Instead of return corr return cls(pattern=corr)

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

Suraj Shah
Suraj Shah
1,055 Points

Hi Chris,

This gave me some good steer but I had follow up questions:

what is the logic behind return cls(pattern=corr). I think I don't understand how the return cls works, therefore I cannot understand why you would equate pattern = cord

I have the following code:

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, text):
        convert_to_list = text.split(-)
        reformatted = []
        for item in convert_to_list:
            if item == "dash":
                reformatted.append(".")
            elif item == "dot":
                reformatted.append("_")
            else:
                continue
        return cls(pattern = reformatted)

[MOD: fixed ```python formatting -cf]

Chris Freeman
Chris Freeman
Treehouse Moderator 68,454 Points

The pattern cls(pattern=reformatted) says:

  • "cls" is the class cls passed to the classmethod. As a class, it can be called like a function to create a new instance of that class
  • "pattern=reformatted" is the argument list to be used by cls.__init__ to create the new instance.