-3

i want to create a StringList class where i can call following functions:

s1 = StringList()
s1.strings.append('a string')

a string has to be created as a list, and i want to be able to add strings to that list and later print out the string plus the lengths of the string

so far i created the class // thx to the answer i edited it so far

 class StringList:
    def __init__(self):
        self.strings = []
        self.lenghts = 0

edit: so far i have (thx to comments and solution)

class StringList:
    def __init__(self):
        self.strings = []


    def add_string(self, string_value):
        self.strings.append(string_value)

which is able to do

s1.add_string('a string')

now comes my problem with lengths. i cant get it to work.

when using it like this:

 class StringList:
    def __init__(self):
        self.strings = []
    def add_string(self, string_value):
        self.strings.append(string_value)
    def lengths(self):
        return len(self.string_value)

following shows up

>>> s1 = StringList()
>>> s1.add_string('a string')
>>> print(s1.strings, s1.lengths)
['a string'] <bound method StringList.lengths of <__main__.StringList object at 0x03566CB0>>
  • `def __init__(self, strings, lenghts):` you need to pass the constructor values for `strings` and `lenghts` (which is misspelled). Or you can have default values like `def __init__(self, strings=[], lenghts=0):` Also, there can only be one `StringList.lenghts`. You cannot have both a method of that name and an attribute of that name – Patrick Haugh Jan 30 '17 at 16:32
  • 1
    @PatrickHaugh Be careful advising people to use an empty list as a default argument (http://stackoverflow.com/questions/1132941/least-astonishment-and-the-mutable-default-argument) – khelwood Jan 30 '17 at 16:35
  • I have the feeling you're still trying to get the grasp on OOP. If you want `s1.strings.append('a string')` then `strings` has to be a `list`. I think what you really want is a method `append` so that you can call `s1.append('a string')`. – Matthias Jan 30 '17 at 16:35
  • BTW, you are spelling 'lenghts' wrong (should be 'lengths') – Tom Burrows Jan 30 '17 at 16:41
  • 1
    The code for `add_string(self, string_value)` is `self.strings.append(string_value)` – Matthias Jan 30 '17 at 16:43
  • Have a read of [this tutorial](http://www.programarcadegames.com/index.php?chapter=introduction_to_classes&lang=en) to learn about classes, or just find any other tutorial you like! – Tom Burrows Jan 30 '17 at 17:06

1 Answers1

0

It isn't entirely clear what you are asking, but here goes:

Is self.strings supposed to be a list? As I understand it, self.strings is supposed to be a list that you can put stuff in both when you create the class (i.e. parameters) and using sl.strings.append('a string'). Is this correct, or do you only want the latter functionality?

If you only want the latter (which I suspect is the case), then quite simply define your class like this:

class StringList:
    def __init__(self, lenghts):
        self.strings = []

Then you can freely run sl.strings.append('a string') to add strings.

If you want the both aforementioned functionalities, then you have do do it like this:

class StringList:
    def __init__(self, strings=None, lenghts):
        self.strings = strings if strings is not None else []

The default arguments are set up so that if you don't pass a parameter when you create an instance of StringList, then it will assign the parameter []. If you do pass something as an argument, it will have to be a list for you to later be able to run sl.strings.append('a string')

The strings=None default argument is used because using an empty list as a default argument is dangerous.

Community
  • 1
  • 1
Tom Burrows
  • 2,225
  • 2
  • 29
  • 46
  • @mistergreen: I have a feeling you might read the wrong book for learning Python. Which one do you use? – Matthias Jan 30 '17 at 17:00
  • i am using standard python 3 documentation, but i cant figure it out how to use it for the given task – mistergreen Jan 30 '17 at 17:02
  • @mistergreen I've read your new problem, but you'll have to post the code you are calling for `add_string`. – Tom Burrows Jan 30 '17 at 17:04
  • If you read the documentation like you read the comments to your question it might take you a while. I wrote the code for the `add_string` method 20 minutes ago in the comments ... – Matthias Jan 30 '17 at 17:05
  • when adding it like you suggested i get the same error – mistergreen Jan 30 '17 at 17:06
  • @mistergreen The problem you currently have is because you are trying to print `s1.lengths`, which is a method. If you want to print what the method returns, run `print(s1.lenghts())` – Tom Burrows Jan 31 '17 at 12:32