Proposal: named return values through dict initialization and unpacking

2016-06-21 Thread Ari Freund via Python-list
I'd like to run this idea by the community to see if it's PEP worthy and
hasn't been already rejected.

Background
Just as keyword arguments enhance code readability and diminish the risk of
bugs, so too would named
return values.  Currently, we can write
   val1, val2, val3 = myfunc()
but we must take care to order the variables correctly, i.e., in the same
order as the corresponding
values in myfunc's return statement.  Getting it wrong may result in
difficult-to-detect bugs.  This
problem would be alleviated if we could name the return values and
disregard their order.

My proposal consists of two parts.

1.  Add a new form of assignment by dictionary unpacking.
  Suppose d is a dict whose keys are the strings 'var1', 'var2',
'var3'.  Then the assignment
 var1, var2, var3 = **d
  would set the variables to the corresponding values.  Order would not
be important, so
 var3, var1, var2 = **d
  would have the same effect.
  Also, not all keys would need to participate; for example,
 var2 = **d
  would work just fine.
  It would be an error for the same name to appear more than once on
the LHS of the assignment.
  It would be an error for a name on the LHS to not be a valid variable
name or for there not to
  be a corresponding key in the dict.
  It would (possibly) be an error if the dict contained keys that are
not strings (or are strings
  that cannot serve as variable names).

2.  Extend the dict() form of dictionary initialization to allow an element
to be a variable name, e.g.,
 dict(var1, var2, var3, var4=33)
  would be equivalent to
 dict(var1=var1, var2=var2, var3=var3, var4=33)

I believe each of the above is useful in its own right.  Additionally,
their combination yields reasonably
streamlined named return value functionality, as in

 def RunShellCmd(cmd):
exit_code, output = commands.getstatusoutput(cmd)  # Here we need
to get the order right.
return dict(exit_code, output)

 output, exit_code = **RunShellCmd("ls .")  # Here we don't care about
order.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Proposal: named return values through dict initialization and unpacking

2016-06-26 Thread Ari Freund via Python-list
Thanks everybody.  There seems to be a lot of resistance to dict unpacking, in 
addition to the problem with my proposed shorthand dict() initialization syntax 
pointed out by Steven D'Aprano, so I won't be pursuing this.
-- 
https://mail.python.org/mailman/listinfo/python-list