Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SIM108: Use the ternary operator if it's reasonable. #12

Closed
MartinThoma opened this issue Oct 10, 2020 · 1 comment
Closed

SIM108: Use the ternary operator if it's reasonable. #12

MartinThoma opened this issue Oct 10, 2020 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@MartinThoma
Copy link
Owner

Explanation

Use the ternary operator if it's reasonable.

Example

# Bad
if a:
    b = c
else:
    b = d

# Good
b = c if a else d
@MartinThoma MartinThoma added the enhancement New feature or request label Oct 10, 2020
@MartinThoma MartinThoma self-assigned this Oct 10, 2020
@MartinThoma
Copy link
Owner Author

astpretty --no-show-offsets /dev/stdin <<< `cat example.py` 
Module(
    body=[
        If(
            test=Name(id='a', ctx=Load()),
            body=[
                Assign(
                    targets=[Name(id='b', ctx=Store())],
                    value=Name(id='c', ctx=Load()),
                    type_comment=None,
                ),
            ],
            orelse=[
                Assign(
                    targets=[Name(id='b', ctx=Store())],
                    value=Name(id='d', ctx=Load()),
                    type_comment=None,
                ),
            ],
        ),
    ],
    type_ignores=[],
)

@MartinThoma MartinThoma changed the title [New Rule] Use ternary operator [New Rule] SIM108: Use ternary operator Oct 10, 2020
@MartinThoma MartinThoma changed the title [New Rule] SIM108: Use ternary operator [New Rule] SIM108: Use the ternary operator if it's reasonable. Oct 10, 2020
@MartinThoma MartinThoma changed the title [New Rule] SIM108: Use the ternary operator if it's reasonable. SIM108: Use the ternary operator if it's reasonable. Jan 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant