[Tutor] passing form data into a class

tpc247 at gmail.com tpc247 at gmail.com
Tue Jul 31 05:32:55 CEST 2007


dear fellow Python enthusiasts, let's say I have a dictionary of keys and
values obtained from a form submitted by a user.  For each submitted form I
will create the Application and Candidate classes, and I want to be able to
call Application(Candidate(**submitted_data)).display() to create an html
representation of the user submitted data.  Depending on the applicant's
preferences and location, the number of fields on the form, and thus the
size of this dictionary, is variable, and not all fields are required.  I
seem to recall a setargs function in Python that would take any argument
passed into a class and set that argument as a member variable in a class.
A Google search turned up no such construct, so here I am asking you, is
there a way for me to create a class that accepts a dictionary of submitted
data and uses each key ad value to create a corresponding member variable ?
The current way I do this is with a very long argument list, and line by
line:

class Candidate(object):
    def __init__(self, full_name, address_line_1, city, postal_code, email,
desired_program, telephone=None, disabled=None, can_receive_media=None,
media_sent_to=None, ...):
        self.full_name = full_name
        self.address_line_1 = address_line_1
        self.city = city
        self.postal_code = postal_code
        self.email = email
        self.desired_program = desired_program

class Application(object):
    def __init__(self, candidate):
        self.candidate = candidate

    def display(self):
        pass
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070730/c0e3f183/attachment.html 


More information about the Tutor mailing list