Skip to content

WIP: use assign expr on if#8116

Closed
vstinner wants to merge 2 commits into
python:masterfrom
vstinner:wip_if_assign_expr
Closed

WIP: use assign expr on if#8116
vstinner wants to merge 2 commits into
python:masterfrom
vstinner:wip_if_assign_expr

Conversation

@vstinner

@vstinner vstinner commented Jul 5, 2018

Copy link
Copy Markdown
Member

Replace:

var = expr
if var:
    ...

with:

if (var := expr):
    ...

Don't replace when:

  • var is used after the if
  • Code like var = regex.match(); if var: return var.group(1)
    is left unchanged since it's covered by a dedicated PR.

Sometimes, var is only used in the condition and so has been removed
in this change. Maybe such kind of pattern should be addressed in a
different pull request.

This patch is restricted to the simplest test if var:, other
conditions like if var > 0: are left unchaned to keep this change
reviewable (short enough).

Replace:

    var = expr
    if var:
        ...

with:

    if (var := expr):
        ...

Don't replace when:

* var is used after the if
* Code like "var = regex.match(); if var: return var.group(1)"
  is left unchanged since it's covered by a dedicated PR.

Sometimes, var is only used in the condition and so has been removed
in this change. Maybe such kind of pattern should be addressed in a
different pull request.

This patch is restricted to the simplest test "if var:", other
conditions like "if var > 0:" are left unchaned to keep this change
reviewable (short enough).
@vstinner

Copy link
Copy Markdown
Member Author

The PEP 572 has been accepted. I only wrote these WIP PEPs to help me to discuss the PEP. I never intended to propose these changes to be merged. So I close this PR.

@vstinner vstinner closed this Jul 12, 2018
@vstinner vstinner deleted the wip_if_assign_expr branch July 12, 2018 07:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants