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

Navin Kabra navin.kabra at gmail.com
Fri May 23 16:04:52 CEST 2014


"Mandar Vaze / मंदार वझे" <mandarvaze at gmail.com> writes:

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

First of all, both functions are returning a single value, a single
dict. So the entire discussion of "how many values should I return from a
function" is irrelevant.

As for which of the two code samples is better, I think it is a matter
of taste.

However, I should point out that in template programming (as happens in
the case of Django), it is fairly common to create and return a dict
containing all the values that are needed for the template. In other
words, it is a pattern that people are used to; and since they're all
values that are needed in the same template, it is OK to club them
together like this. To me, "Code #1" is perfectly acceptable - for this
use case. 

> Code 2:
> user = {}
[ 5 more citation lines. Click/Enter to show. ]
> user['fname'] = fname
> user['lname'] = lname
> ...
> ...
> ...
> user['country'] = country
>
> return dict(user=user)

I don't see why doing the dict assignments into different statements is
any better than simply creating the dict directly. In fact, I prefer the
former (but, I would put each "key: value" pair on a line by itself),
because the intent is much clearer.

Also, you could have done "return user" here instead of "return
dict(user=user)". Which one is better has nothing to do with python, and
depends upon the template language you're using and the content of the
template.

navin.


More information about the BangPypers mailing list