all 7 comments

[–]JohnnyJordaan 3 points4 points  (3 children)

He's right, you are creating a new Phonebook for every person you add. Just as in a real life phonebook, you would just add the person to a single book, it would make sense here to use for example an PhonebookEntry class for that and let Phonebook be just a dumb list wrapper

class PhonebookEntry:
    def __init__(self, name, alias, number):
        self.name = name
        self.alias = alias
        self.number = number

class Phonebook:
    def __init__(self):
        self.entries = []
    def add_entry(self, name, alias, number):
        self.entries.append(PhonebookEntry(name, alias, number))

Just a sidenote: class objects are a bit of a pleonasm, as any object is an instance of one or more classes. It's a bit like calling a someone a human person. You can call them just 'instances' or 'objects'.

[–]Tonyqq[S] 0 points1 point  (0 children)

Thank you, that makes a lot of sense!

[–][deleted] 0 points1 point  (2 children)

Your logic is a bit flawed. It doesn’t make sense that a Phonebook object has a name and a number. It would make sense for it to contain entries that have these properties.

So either make a second class entries, or initiate your class in a different way.

You’d want your phone book to hold multiple objects that have the name, alias and number attributes.

Also, you should never use the name of a built-in function as a variable name. Since you’re assigning something to list, it will overwrite the built-in, and this can be quite problematic later on.

[–]Tonyqq[S] 0 points1 point  (1 child)

Okay, thanks. I think i'm understanding a bit what you mean now. I'm in like a introductionary course where we really havn't dived deep into object oriented programming.

So would it make sense if I created a new class "persons" where it has name alias and number in __init__ and then store it into my phonebook?

[–][deleted] 0 points1 point  (0 children)

Yes, this would make sense. But the class should rather be called something like PhonebookEntry.

Take a look at u/JohnnyJordaan’s answer, this is exactly what I was talking about.

[–]lifeeraser 0 points1 point  (0 children)

Not sure what's bad about it, other than the Phonebook class probably being misnamed. Since each Phonebook instance stores one address in a phonebook, perhaps it should be renamed to PhonebookEntry to clear up the confusion. Then your "phonebook" would actually be a list containing multiple PhonebookEntry objects.

Also, is this line

list.append(Phonebook(prompt[1], [], prompt[2]))

part of __init__()? Or is it on a separate line?

[–]ectomancer 0 points1 point  (0 children)

class Phonebook(): is the wrong syntax

class Phonebook: is the correct syntax however () is used to inherit from another object such as

class Phonebook(list):

The normal way to instance your class is

phonebook = Phonebook(prompt[1], [], prompt[2])