[Python-ideas] Respectively and its unpacking sentence

Sjoerd Job Postmus sjoerdjob at sjec.nl
Wed Jan 27 00:57:43 EST 2016


On Wed, Jan 27, 2016 at 12:25:05AM -0500, Mirmojtaba Gharibi wrote:
> Hello,
> 
> I'm thinking of this idea that we have a pseudo-operator called
> "Respectively" and shown maybe with ;

Hopefully, you're already aware of sequence unpacking? Search for
'unpacking' at https://docs.python.org/2/tutorial/datastructures.html .
Unfortunately, it does not have its own section I can directly link to.

    x, y = 3, 5

would give the same result as

    x = 3
    y = 5

But it's more robust, as it can also deal with things like

    x, y = y + 1, x + 4
> 
> 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 what would happen with the following?

    a; b = x1;a + x2;5

> 
> 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]
> 

>From what I can see, it would be very beneficial for you to look into
numpy: http://www.numpy.org/ . It already provides inner product, sums
of arrays and such. I myself am not very familiar with it, but I think
it provides what you need.

> 
> 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.

Again, probably you should use numpy. I'm not really sure it warrants a
change to the language, because it seems like it would really only be
beneficial to those working with matrices. Numpy already supports it,
and I'm suspecting that the use case for `a;b = c;d + e;f` can already
be satisfied by `a, b = c + e, d + f`, and it already has clearly
documented semantics and still works fine when one of the names on the
left also appears on the right: First all the calculations on the right
are performed, then they are assigned to the names on the left.

> 
> Cheers,
> Moj

Kind regards,
Sjoerd Job


More information about the Python-ideas mailing list