0

The appearance of the new := operator in python 3.8 opens a huge field of opportunities for comprehensions and generator expressions, e.g. (a Fibonacci suite comprehension):

[i0 := 0, i1 := 1]+[i1 := i0 + (i0 := i1) for j in range(2,10)]

However, it might also open the need for an equivalent to the c , operator (which evaluate both the left hand and right hand expression and returns the result of the later) when you want to create data which you will use later, but it is not part of the result. eg : Let's assume we have an operator which would be a python equivalent to the c's comma operator. making a comprehension for a suite of products sum times difference of the consecutive fibonacci number, would probably require something like :

[i0 := 0 ‡ i1 := 1 ‡ (i1+i0)(i1-i0)]+[i1 := i0 + (i0 := i1) ‡ (i1+i0)(i1-i0) for j in range(2,10)]

So, how could we make something like this comma operator in python ?

Camion
  • 1,264
  • 9
  • 22

1 Answers1

0

We just need to use the normal python's comma to make a tuple (or a list if we need it to be mutable), and select the last element (-1).

[(i0 := 0, i1 := 1, (i1+i0)*(i1-i0))[-1]]+[(i1 := i0 + (i0 := i1), (i1+i0)*(i1-i0))[-1] for j in range(2,10)]
Camion
  • 1,264
  • 9
  • 22