[Tutor] How do I make a variable that refers to an object have the same label as the object's name ?
tpc247 at gmail.com
tpc247 at gmail.com
Thu Oct 5 11:14:29 CEST 2006
question for the Python crowd:
when creating a series of objects, where each object represents a field from
a submitted html form, how do I make variables that reference the objects be
the same as the names of the respective objects ?
For example, I have a class called Datum:
class Datum:
def __init__(self, key, value, required=None, label=None):
self.key = key
self.value = value
self.required = required
self.label = label
and a dictionary of submitted values:
some_dict = {'applicant_full_name':'John Smith',
'applicant_address_line_1':'100 Main Street', 'applicant_city':'Anytown',
'applicant_state':'WY', 'applicant_postal_code':'55555', 'country':'US', '
applicant_email':'jsmith at server.gov', 'applicant_telephone':'555 555 5555'}
and I created a list of objects like so:
some_list = []
for key, value in some_dict.items():
some_datum = Datum(key, value)
some_list.append(some_datum)
so now I have a list of Datum instances called some_list, and I would like
to make it so I can refer to each respective Datum instance by its Datum.key,
e.g., applicant_full_name would refer to the Datum instance with key
'applicant_full_name' and applicant_full_name.value would give me "John
Smith." I tried:
for x in some_list:
some_string = "%s = %s" % (x.key, x)
eval(some_string)
and got:
Traceback (most recent call last):
File "<pyshell#98>", line 4, in -toplevel-
eval(some_string)
File "<string>", line 1
applicant_state = <__main__.Datum instance at 0x009745F8>
^
SyntaxError: invalid syntax
Am I doing something wrong ?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20061005/9bb92b99/attachment.htm
More information about the Tutor
mailing list