I’ve started self-learning python and while I know C# and Java the python language is a bit strange for me right now. I’d like to make a small phonebook program.
I want to make a list called phonebook where each member in the list is from a class called entery, in each entery there are several strings and a list of strings for phone numbers. For example:
class entery:
def addEntery (str1,str2,list):
tmp = entery()
tmp.str1 = str1
tmp.str2 = str2
tmp.list = list
def main():
phonebook = []
phonebook.append(addEntery('ian1','ian2',list)
I need help on closing the gap i have from C# to python, and making a class for simple tasks.
You’re not defining a proper constructor. It should be:
Since Python doesn’t have “private” data, you can access each of these fields directly from the “outside”:
Then you can build a list by calling the constructor to build instances:
This is quite important:
phonebookis a list of instances, i.e. a list of objects, not a “list of classes”. It’s of course possible to put classes in a list too, but that’s not what you’re after.You probably should focus more on reading some basic Python class tutorial. Also, the word you’re looking for when it comes to naming the class is probably
Entry.