[Python-ideas] Respectively and its unpacking sentence

Mirmojtaba Gharibi mojtaba.gharibi at gmail.com
Wed Jan 27 00:25:05 EST 2016


Hello,

I'm thinking of this idea that we have a pseudo-operator called
"Respectively" and shown maybe with ;

Some examples first:

a;b;c = x1;y1;z1 + x2;y2;z2
is equivalent to
a=x1+x2
b=y1+y2
c=z1+z2

So it means for each position in the statement, do something like
respectively. It's like what I call a vertical expansion, i.e. running
statements one by one.
Then there is another unpacking operator which maybe we can show with $
sign and it operates on lists and tuples and creates the "Respectively"
version of them.
So for instance,
vec=[]*10
$vec = $u + $v
will add two 10-dimensional vectors to each other and put the result in vec.

I think this is a syntax that can make many things more concise plus it
makes component wise operation on a list done one by one easy.

For example, we can calculate the inner product between two vectors like
follows (inner product is the sum of component wise multiplication of two
vectors):

innerProduct =0
innerProduct += $a * $b

which is equivalent to
innerProduct=0
for i in range(len(a)):
...innerProduct += a[i]+b[i]


For example, let's say we want to apply a function to all element in a
list, we can do:
f($a)

The $ and ; take precedence over anything except ().

Also, an important thing is that whenever, we don't have the respectively
operator, such as for example in the statement above on the left hand side,
we basically use the same variable or value or operator for each statement
or you can equivalently think we have repeated that whole thing with ;;;;.
Such as:
s=0
s;s;s += a;b;c; * d;e;f
which result in s being a*d+b,c*e+d*f

Also, I didn't spot (at least for now any ambiguity).
For example one might think what if we do this recursively, such as in:
x;y;z + (a;b;c);(d;e;f);(g;h;i)
using the formula above this is equivalent to
(x;x;x);(y;y;y);(z;z;z)+(a;b;c);(d;e;f);(g;h;i)
if we apply print on the statement above, the result will be:
x+a
x+b
x+c
y+d
y+e
y+f
z+g
z+h
z+i

Beware that in all of these ; or $ does not create a new list. Rather, they
are like creating new lines in the program and executing those lines one by
one( in the case of $, to be more accurate, we create for loops).

I'll appreciate your time and looking forward to hearing your thoughts.

Cheers,
Moj
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160127/346cbd85/attachment.html>


More information about the Python-ideas mailing list