[Python-ideas] Respectively and its unpacking sentence

Mirmojtaba Gharibi mojtaba.gharibi at gmail.com
Fri Jan 29 11:34:18 EST 2016


On Fri, Jan 29, 2016 at 11:04 AM, Pavol Lisy <pavol.lisy at gmail.com> wrote:
> I would really like
>
> (a;b;c) in L
>
> vs
>
> a in L and b in L and c in L
>
> or
>
> all(i in L for i in (a,b,c))
>
> because readability very matters.
>
> But if I understand then this is not what we could get from your
> proposal because a;b;c is not expression. Right?
>
> So we have to write something like
>
> vec=[None]*3
> vec=(a;b;c) in L
> all(vec) # which is now equivalent to (a in L and b in L and c in L)
>

That's right. Instead, we can get it this way:
a;b;c = $L
which is equivalent to
a=L[0]
b=L[1]
c=L[2]

but as others suggested for this particular example, we can already
get it from unpacking syntax, i.e.
a,b,c = *L

>
>> vec=[]*10
>> $vec = $u + $v
>
> First row is mistake (somebody wrote it yet) but why not simply? ->
>
> vec = list($u +$v)
>
> Because $u+$v is not expression. It is construct for "unpacking"
> operations. It could be useful to have "operator" (calling it operator
> is misleading because result is not object) to go back to python
> variable. (but with which type? tuple?) Probably $(a;b) could
> ("return") be transformed to tuple(a,b)
>
> a=[1,2]
> print(a)
> print($a)
> print($$a)
> ----
> [1,2]
> 1
> 2
> (1,2)
>
> So I could write
>
> a in L and b in L and c in L
>
> as
>
> all($((a;b;c) in L))           # which is much less nice as "(a;b;c) in L"
>
> and
>
> all(i in L for i in (a,b,c))   # which is similar readable and don't
> need language changes
>
>> s=0
>> s;s;s += a;b;c; * d;e;f
>> which result in s being a*d+b,c*e+d*f
>
> do you mean a*d+b*d+c*f ?

Yes, Oops, it was a typo.

>
> -------
>
> Your idea is interesting. If you like to test it then you could
> improve implementation of next function and play with it (probably you
> will find some caveats):
>
> def respectively(statement):
>   if statement!='a;b=b;a':
>     raise SyntaxError("only supported statement is 'a;b=b;a'")
>   exec('global a\nglobal b\na=b\nb=a')
>
> a,b=1,2
> respectively('a;b=b;a')
> print(a,b)
> 2 2
>
> Unfortunately this could work only in global context due to
> limitations around 'exec' and 'locals' functions.

Sounds good. I'd like to experiment with it actually.


More information about the Python-ideas mailing list