On Mon, Aug 17, 2020 at 8:11 AM Mark Shannon <mark@hotpy.org> wrote:


On 17/08/2020 3:08 pm, Henk-Jaap Wagenaar wrote:
> Thanks for having a look! The example now looks like (looking at int
> case only, same applies to UID):
>
>          case int():
>              if value < 0:
>                  try:
>                      self._fp.write(struct.pack('>Bq', 0x13, value))
>                  except struct.error:
>                      raise OverflowError(value) from None
>              elif value < 1 << 8:
>                  self._fp.write(struct.pack('>BB', 0x10, value))
>              ...
>              elif value < 1 << 64:
>                  self._fp.write(b'\x14' + value.to_bytes(16, 'big',  signed=True))
>              else:
>                  raise OverflowError(value)
>
> I was more thinking it would read/look something like:
>
>          case int() if value < 0:
>                  try:
>                      self._fp.write(struct.pack('>Bq', 0x13, value))
>                  except struct.error:
>                      raise OverflowError(value) from None
>          case int() if value < 1 << 8:
>                  self._fp.write(struct.pack('>BB', 0x10, value))
>          ...
>          case int() if value < 1 << 64:
>                  self._fp.write(b'\x14' + value.to_bytes(16, 'big',  signed=True))
>          case int():
>              raise OverflowError(value)
>
> Which I think works as expected under the current PEP622?

That would work, but would be slower for the reference implementation
due to the repeated `isinstance(value, int)` checks.

The PEP allows the compiler to generate optimized code that only checks once.
 
I think the repeated `int()` cases do not help readability.
Which form do you think is more readable?

I find Henk-Jaap's version better, because the case blocks show the structure of the code better.

--
--Guido van Rossum (python.org/~guido)