[Tutor] passing form data into a class

Eric Brunson brunson at brunson.com
Tue Jul 31 18:47:44 CEST 2007

Dave Kuhlman wrote:
> On Mon, Jul 30, 2007 at 08:32:55PM -0700, tpc247 at gmail.com wrote:
>> 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:
> You have already gotten one good solution from Kent.  But,
> depending on your needs, you might also focus on the request side
> rather than on when the class or instance is created.  That might
> enable you to add a bit of the security checking that Ken Fouey was
> concerned about.
> Here is an example of a class which looks a name up in a dictionary
> when the attribute is requested.  It does not create attributes for
> the keys in the dictionary:
>     class Bunch(object):
>         def __init__(self, vardict=None):
>             if vardict is None:
>                 self.vardict = vardict
>             else:
>                 self.vardict = vardict
>         def __getattr__(self, name):
>             if name in self.vardict:
>                 return self.vardict[name]
>             else:
>                 raise AttributeError, 'Bunch has no attribute: %s' % name

Since this is tutor, I'll add my $.02...

Kent's version of Bunch is great, I use that sort of definition all the 
time in my code when I want to use "instance.thing = blah" to save 
typing and make my code more readable, but also want to use dict 
nomenclature for iteration over keys, etc, rather than "getattr( 
instance, key )," again, primarily for neatness of code.

However, this version from Dave has the advantage of segregating your 
"special data" from the regular class attributes.  If you wanted to, for 
example, keep a count of the number of form keys that were passed into 
the form, you couldn't store that in an instance attribute without it 
getting mixed up with all your form variables.

I'm not saying either is better than the other overall, they each have 
their place and I use both, but in this *specific* case, I think you'd 
want to use Dave's version.

Just my opinion, I could be wrong.  :-)


>     def test():
>         d = {'aaa': 111, 'bbb': 222, }
>         b = Bunch(d)
>         print b.aaa
>         print b.ccc
>     test()
> Dave

More information about the Tutor mailing list