[Python-Dev] Assignment expression and coding style: the while True case (original) (raw)
MRAB python at mrabarnett.plus.com
Wed Jul 4 21:33:44 EDT 2018
- Previous message (by thread): [Python-Dev] Assignment expression and coding style: the while True case
- Next message (by thread): [Python-Dev] Assignment expression and coding style: the while True case
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 2018-07-05 01:11, Victor Stinner wrote:
The code comes from Lib/pyio.py. Simplified code: --- nodataval = b"" ... if n is None or n == -1: ... currentsize = 0 while True: chunk = self.raw.read() if chunk in emptyvalues: nodataval = chunk break currentsize += len(chunk) chunks.append(chunk) return b"".join(chunks) or nodataval
... while avail < n:_ _chunk = self.raw.read(wanted)_ _if chunk in emptyvalues:_ _nodataval = chunk_ _break_ _avail += len(chunk)_ _chunks.append(chunk)_ _..._ _return out[:n] if out else nodataval_ _---_ _It seems like "nodataval = " assignment can be moved out of the first_ _loop, but cannot be moved for the second loop (since the second loop_ _has no iteration if "avail >= n"). Yeah, maybe for this specific file, assignment expressions could be used for the (C) case and would be worth it. In this case, the second loop might be better left as-is because there are 2 conditions for leaving the loop. Stylistically, it might be starting to hurt readability with something like:
while avail < n or (chunk := self.raw.read(wanted)) not in empty_values:
[snip]
- Previous message (by thread): [Python-Dev] Assignment expression and coding style: the while True case
- Next message (by thread): [Python-Dev] Assignment expression and coding style: the while True case
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]