Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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 67,986 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 67,986 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.