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

Mandar Vaze / मंदार वझे mandarvaze at gmail.com
Fri May 23 08:51:11 CEST 2014


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)

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

Code 2:

user = {}
user['fname'] = fname
user['lname'] = lname
...
...
...
user['country'] = country

return dict(user=user)


I was told first style is more "readable" since it tells you what is being
passed to the template (I disagree, but it is subjective/personal opinion)

Queries :

1. Is there a *limit* on "How many values can be returned from a function" ?
2. What is *performance impact* of returning so many values - (push and pop
from the stack)
3. Based on #1 and #2 is there a *recommendation* on "where to draw the
line"

Thanks,
-Mandar


More information about the BangPypers mailing list