Python Forum
rework of a little python-script - extending getters and setters
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
rework of a little python-script - extending getters and setters
#8
(Nov-04-2018, 11:51 AM)apollo Wrote: thx for the answer - finally got there
You 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 Angel
"""
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.
Reply


Messages In This Thread
RE: rework of a little python-script - extending getters and setters - by snippsat - Nov-04-2018, 01:37 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Extending list doesn't work as expected mmhmjanssen 2 324 May-09-2024, 05:39 PM
Last Post: Pedroski55
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,670 Jun-29-2023, 11:57 AM
Last Post: gologica
  Can property getters and setters have additional arguments? pjfarley3 2 3,141 Oct-30-2020, 12:17 AM
Last Post: pjfarley3
  How to kill a bash script running as root from a python script? jc_lafleur 4 6,139 Jun-26-2020, 10:50 PM
Last Post: jc_lafleur
  crontab on RHEL7 not calling python script wrapped in shell script benthomson 1 2,407 May-28-2020, 05:27 PM
Last Post: micseydel
  Package python script which has different libraries as a single executable or script tej7gandhi 1 2,709 May-11-2019, 08:12 PM
Last Post: keames
  Extending my text file word count ranker and calculator Drone4four 8 5,500 Jan-25-2019, 08:25 AM
Last Post: steve_shambles
  Check Python version from inside script? Run Pythons script in v2 compatibility mode? pstein 2 9,936 Jul-07-2017, 08:59 AM
Last Post: snippsat
  getters and setters Skaperen 10 8,522 May-16-2017, 07:13 AM
Last Post: Ofnuts
  Cant pass corect variables to python script in bash script neradp 3 6,317 Nov-05-2016, 01:26 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020