1

Let's say Bob enters his name, 'Bob Jones,' into a Tkinter text entry field. Later, I want to be able to access his name like this:

BobJones = {
'name':'Bob Jones',
'pet\'s name':'Fido'
}

How do I make it so I assign whatever Bob inputs as a new variable without having to manually write it in?

Thanks in advance.

Eli Dinkelspiel
  • 769
  • 1
  • 6
  • 15

1 Answers1

1

To expand on the comments above, I think you'd want something more like this:

# Define the user class - what data do we store about users:

class User(object):
    def __init__(self, user_name, first_pet):
        self.user_name = user_name
        self.first_pet = first_pet

# Now gather the actual list of users

users = []
while someCondition:
    user_name, first_pet = getUserDetails()
    users.append(User(user_name, first_pet))

# Now we can use it

print "The first users name is:"
print users[0].user_name

So you're defining the class (think of it as a template) for the Users, then creating new User objects, one for each person, and storing them in the users list.

Tom Dalton
  • 6,122
  • 24
  • 35
  • wow, I can't believe I forgot about classes! thanks a lot, man – Eli Dinkelspiel Apr 09 '14 at 00:50
  • No problem. If you're after sometihng lighter-weight than this (and the data will never be changed 'immutable'), you could use Named Tuples instead of classes, e.g. http://stackoverflow.com/questions/2970608/what-are-named-tuples-in-python – Tom Dalton Apr 09 '14 at 00:51
  • No I definitely need to use classes. I'm storing ~50 data points per user. – Eli Dinkelspiel Apr 09 '14 at 00:53