Nov-04-2018, 01:37 PM
(Nov-04-2018, 11:51 AM)apollo Wrote: thx for the answer - finally got thereYou still don't lissen to advice about getters and setters.
If i delete(getters/setters) and do some PEP-8 fixes,30 lines are gone
""" contacts.py This program uses a Person class to keep track of contacts. """ class Person(object): """ The Person class defines a person in terms of a name, phone number, and email address. """ def __init__(self, name, phone, email, padress): self.name = name self.phone = phone self.email = email self.padress = padress def __str__(self): return "Person[name={}, phone={}, email={}, address={}]".format( self.name, self.phone, self.email, self.padress ) def enter_a_friend(): name = input("Enter friend's name: ") phone = input("Enter phone number: ") email = input("Enter email address: ") padress = input("Enter friend's padress:") return Person(name, phone, email, padress) def lookup_a_friend(friends): found = False name = input("Enter name to lookup: ") for friend in friends: if name in friend.name: print(friend) found = True if not found: print("No friends match that term") def show_all_friends(friends): print("Showing all contacts:") for friend in friends: print(friend) def main(): friends = [] running = True while running: print("\nContacts Manager") print("1) new contact 2) lookup") print("3) show all 4) end ") option = input("> ") if option == "1": friends.append(enter_a_friend()) elif option == "2": lookup_a_friend(friends) elif option == "3": show_all_friends(friends) elif option == "4": running = False else: print("Unrecognized input. Please try again.") print("Program ending.") if __name__ == "__main__": main()Python Is Not Java
Quote:Getters and setters are evil. Evil, evil, I say! Python objects are not Java beans.
Do not write getters and setters.
This is what the 'property' built-in is for. And do not take that to mean that you should write getters and setters, and then wrap them in 'property'.
That means that until you prove that you need anything more than a simple attribute access, don't write getters and setters.
They are a waste of CPU time, but more important, they are a waste of programmer time.
Not just for the people writing the code and tests, but for the people who have to read and understand them as well.