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

Morse.py, methods, calling, classes... trouble grasping a concpet of classses

So, I'm having a real hard time of grasping the concept of classess, methods, attributes etc.

I'm starting to do the morse.py challenge and I stumble upon an easier problem. I've rewatched all the videos but I still don't understand it.

Let's assume I have a simple code ( I know it does not fit all the requirements)

class Letter: def init(self, pattern=None): self.pattern = pattern
self.string() return morse

def __str__(pattern):
  morse = ''
  for i in pattern:
    if i is '.':
      morse = morse+'dot'
    else:
      pass
  for i in pattern:
    if i is '_':
      morse = morse+'dash'
    else:
      pass
  return morse

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

HOW to call it in workspaces? How to check if "it works"? Calling "python challenge.py" does not make it work. It does not print anything. So how to call this method inside my class?

I know it is probably an extremly dumb question but im having real trouble with it.

1 Answer

Hi there, not entirely sure what your question is so if I get this wrong - apologies.

firstly if you want to test your code you can create a new instance of the class S and given that str function provides a nicer representation of the class you can check it by printing the class... for instance:

test = S()     # create new instance of the class, which inherits everything from Letter
print(test)   # print out the string representation of the class - 
                    # this calls the __str__ function     

as for the code above a couple of things to think about:

  1. in the str function you do not need to pass in the 'pattern' argument as it is part of the letter class, however, you do have to give it the self argument as it is part of the Letter class.

  2. To iterate through the pattern you just need reference it via self.pattern - for instance

def __str__(self):
    morse = ''
    for i in self.pattern:

thank you!