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
Sean Banko
6,558 PointsCode passed morse code challenge, but the output of print(S.__str__) in workspaces is not correct
My code passed the morse code challenge, but when I run this in workspaces and print(S.str) (that is, S dunder str) the output is "<function Letter.__str__ at 0x7f7bad1e5268>", not "dot-dot-dot". Does anyone know why this happens?
class Letter:
def __init__(self, pattern=None):
self.pattern = pattern
def __str__(self):
my_list = []
for symbol in self.pattern:
if symbol == ".":
my_list.append("dot")
if symbol == "_":
my_list.append("dash")
return "-".join(my_list)
class S(Letter):
def __init__(self):
pattern = ['.', '.', '.']
super().__init__(pattern)
1 Answer
Steven Parker
243,656 PointsYou're printing the reference to the method itself instead of running it and printing the results.
To test the conversion process, you should create an instance of the "S" class, and then print the string representation of it:
print(str(S())) # converting the "S" object to a string runs the __str__ method
print(S()) # this does the same due to implicit type coercion
Sean Banko
6,558 PointsSean Banko
6,558 PointsOh, I think I get it now. Changing the str method for the Letter class changes what happens when an instance of a Letter is coerced into a string, and I hadn't actually created an instance of the letter S to be coerced at all. Thank you!