[BangPypers] Multiple return values from a function : Where to draw the line ?

Noufal Ibrahim KV noufal at nibrahim.net.in
Fri May 23 09:59:15 CEST 2014


On Fri, May 23 2014, Mandar Vaze / मंदार वझे wrote:

> Currently I came across the code that returned 9 values (return statement
> spanned 5 lines due to pep8 limitation of 79 characters per line)
>
> The function returns various values that are used by the template to render
> HTML
>
> To give you an idea - consider following two code snippets :
> (This is just a sample - it might lead you to believe these are various
> columns of a "user" table. Actual code isn't a row from DB table.
> All 9 values are computed, and aren't related to each other)

My usual way of doing this is something like


return dict(fname     = fname,
            lname     = lname,
            saluation = salutation,
            gender    = gender,
            addr1     = addr1,
            addr2     = addr2,
            city      = city,
            state     = state,
            country   = country)

I'm not sure what rules this violates but "it works for me" especially
when I read the code later.

Also, I'm not too sure if you should be worried, atleast on an abstract
level, about the number of keys this dictionary has. If they are all a
single logical coherent unit, it'll fit in the programmers
head. e.g. "an address" makes sense regardless of how many fields it has
(first name, last name, street, pin code etc.). If on the other hand
it's something vague that makes sense only to your own program, it'll be
harder to justify and might require some amount of cleanup.

I'm not sure about the performance impact. There are no "so many
values". You're returning just a (reference to a) single dictionary. The
values will be pulled out of the memory when you access them. 

I don't think this answers your question since it's mostly opinion but
nevertheless...


[...]


-- 
Cordially,
Noufal
http://nibrahim.net.in


More information about the BangPypers mailing list