[New-bugs-announce] [issue42439] Use of ternary operator instead of if and else in month calculation function

Elian Mariano Gabriel report at bugs.python.org
Sun Nov 22 14:46:39 EST 2020


New submission from Elian Mariano Gabriel <elianmarianogabriel at gmail.com>:

Inside the file calendar.py, there are two functions which are supposed to calculate the previous and next month relative to the actual year and month.

def _prevmonth(year, month):
    if month == 1:
        return year-1, 12
    else:
        return year, month-1


def _nextmonth(year, month):
    if month == 12:
        return year+1, 1
    else:
        return year, month+1

Because of the concise calculation that is being made by these functions, it would be convenient to use the ternary operator to calculate that. So, the result would be:

def _prevmonth(year, month):
    return [year-1, 12] if month == 1 else [year, month-1]

def _nextmonth(year, month):
    return [year+1, 1] if month == 12 else [year, month+1]

----------
components: Library (Lib)
files: calendar.py
messages: 381629
nosy: ElianMariano
priority: normal
severity: normal
status: open
title: Use of ternary operator instead of if and else in month calculation function
versions: Python 3.10, Python 3.6, Python 3.7, Python 3.8, Python 3.9
Added file: https://bugs.python.org/file49614/calendar.py

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue42439>
_______________________________________


More information about the New-bugs-announce mailing list