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 trialDenis Chernenko
4,077 PointsNo method named `from_string`
please advice what i've done wrong
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
Treehouse Moderator 68,454 PointsYou have the right idea, but are missing a key element of the task
- The method
from_string
should be a classmethod ofLetter
, notS
- The method should return a new instance with the correct pattern (['_', '.']).
- Instead of return
corr
returncls(pattern=corr)
Post back if you need more help. Good luck!!
Suraj Shah
1,055 PointsSuraj Shah
1,055 PointsHi 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:
[MOD: fixed ```python formatting -cf]
Chris Freeman
Treehouse Moderator 68,454 PointsChris Freeman
Treehouse Moderator 68,454 PointsThe pattern
cls(pattern=reformatted)
says:cls
passed to the classmethod. As a class, it can be called like a function to create a new instance of that classcls.__init__
to create the new instance.