Take the 2-minute tour ×

We have an api that consumes a around 50 to 100 query params. Currently the handler takes all of the params and sets them as attributes in a Meta object. Something like this

meta = Meta()
meta.param1 = param.get('param1', 'somedefault')
meta.param2 = param.get('param2', 'someotherdefault')

and so on. My question is, is there a better way to handle this than just a loooong list of assigns in the handler? My current idea is to just break it out into a helper function.

meta = self.get_meta(param)

Any other ideas?

(updated my example)

share|improve this question
I would suggest using a context object that you pass around. –  Keith Nov 11 '12 at 1:29
@Keith Similar to what we're doing now? –  Matt Nov 11 '12 at 2:27
Oh, i see. Well, considering that you have hard-coded default values that is probably the most clear and maintainable approach. –  Keith Nov 11 '12 at 5:09

3 Answers 3

up vote 2 down vote accepted
    # ...

meta = Meta()
for name in PARAMETERS:
    setattr(meta, name, param[name])

Based on your comment...

    'param1': 1,
    'param2': 'something',
meta = Meta()
for name, value in DEFAULTS.items():
    setattr(meta, name, param.get(name, value))
share|improve this answer
While I would love the simplicity, we often must assign defaults if the value is not present (some are optional but still need a value). –  Matt Nov 11 '12 at 1:50
Easy enough to do –  Jakob Bowyer Nov 11 '12 at 1:54

Expanding on whats been said including default values

    ('param1', "default"),
    ('param2', "default2"),
    # ...

meta = Meta()
for name, default in PARAMETERS:
    setattr(meta, name, param.get(name, default))
share|improve this answer

This seems like something you should do with a mapping instead. Unless you need to filter the parameters, this sounds like a bad idea.

So you'll have something like this:

class Meta(IterableUserDict):

meta = Meta()
share|improve this answer

Your Answer


