0

Very new programmer here, sorry if this sounds stupid. I'm doing an exercise of http://www.swaroopch.com/notes/python/ in which I found that I needed to do something like this:

class Person():
"""Just creates a person with name, address and email"""
    def __init__(self, name, address, email):
        self.name = name
        self.address = address
        self.email = email
persons = {} #dictionary to store various persons as an address book, with each person's name as the key to their object
def changeperson():
"""This is going to be able to access any person in persons, and change their variables"""
    persontochange = raw_input("What person do you want to change?")
    thingtochange = raw_input("What do you want to change?")
    result = raw_input("What should that be?")
    #here assign to persons[persontochange].thingtochange the value of result, I don't know how

Sorry if this sounds stupid and for any problems with my English. Thanks for reading.

chilliefiber
  • 571
  • 2
  • 7
  • 18
  • 1
    Linked duplicate notwithstanding, I would very highly recommend sticking with the dictionary and objects you're already using instead of messing with `setattr`. – TigerhawkT3 Jul 15 '15 at 02:12
  • Thank for linking to the other thread, I had searched but as you see the title is too different. What do you mean by not using setattr? @TigerhawkT3 – chilliefiber Jul 15 '15 at 02:16
  • 1
    I mean work with the data structures you have rather than, in effect, exposing variable names to the user (even though they may not realize it). This could mean something as simple as an `if..elif..else` structure like `if thingtochange.lower().startswith('n'): persons[persontochange].name = thingtochange` and so on. – TigerhawkT3 Jul 15 '15 at 02:42

0 Answers0