[issue44276] Replace if-elif-else structure with match-case (PEP634)
New submission from Kshitiz Arya : Replace if-elif-else with match-case for pattern matching, which is generally faster and more intuitive. -- components: Library (Lib) messages: 394839 nosy: Kshitiz17 priority: normal severity: normal status: open title: Replace if-elif-else structure with match-case (PEP634) type: enhancement versions: Python 3.11 ___ Python tracker <https://bugs.python.org/issue44276> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue44276] Replace if-elif-else structure with match-case (PEP634)
Kshitiz Arya added the comment: Pardon my ignorance here but can I start working on this issue? I am a new contributor therefore I am unsure about how to proceed from here -- ___ Python tracker <https://bugs.python.org/issue44276> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue44276] Replace if-elif-else structure with match-case (PEP634)
Kshitiz Arya added the comment: This is a relatively simple example of how this will improve readability of the code. (This example is form Lib/json/encoder.py) Original - if isinstance(value, str): yield _encoder(value) elif value is None: yield 'null' elif value is True: yield 'true' elif value is False: yield 'false' elif isinstance(value, int): yield _intstr(value) elif isinstance(value, float): yield _floatstr(value) else: if isinstance(value, (list, tuple)): chunks = _iterencode_list(value, _current_indent_level) elif isinstance(value, dict): chunks = _iterencode_dict(value, _current_indent_level) else: chunks = _iterencode(value, _current_indent_level) yield from chunks -- Suggested -- match value: case str(): yield _encoder(value) case None: yield 'null' case True: yield 'true' case False: yield 'false' case int(): # see comment for int/float in _make_iterencode yield _intstr(value) case float(): # see comment for int/float in _make_iterencode yield _floatstr(value) case _: match value: case list()|tuple(): chunks = _iterencode_list(value, _current_indent_level) case dict(): chunks = _iterencode_dict(value, _current_indent_level) case _: chunks = _iterencode(value, _current_indent_level) yield from chunks -- ___ Python tracker <https://bugs.python.org/issue44276> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue44276] Replace if-elif-else structure with match-case (PEP634)
Kshitiz Arya added the comment: I guess we will have to wait until Python 3.10 is released and we have more conclusive data about speed and readability of match-case before we can go ahead with this issue. -- ___ Python tracker <https://bugs.python.org/issue44276> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue44276] Replace if-elif-else structure with match-case (PEP634)
Kshitiz Arya added the comment: I have timed the execution of if-else and match-case on Differ().compare from difflib module and here is what I got When both strings are same ** if-else: 2.504900476196781e-06, match-case: 2.587399649200961e-06, comparisions : 10 if-else: 2.222519979113713e-06, match-case: 1.7874199693324045e-06, comparisions : 100 if-else: 1.954343999386765e-06, match-case: 1.8695319959078916e-06, comparisions : 1000 if-else: 3.2847548005520366e-06, match-case: 1.928162499825703e-06, comparisions : 1 if-else: 1.2241538699890953e-06, match-case: 7.870903900038684e-07, comparisions : 10 if-else: 7.950048359998618e-07, match-case: 7.883418589990469e-07, comparisions : 100 if-else: 7.941918295000505e-07, match-case: 7.882559125006082e-07, comparisions : 1000 if-else: 7.928842861700104e-07, match-case: 7.855620772999828e-07, comparisions : 1 ** When strings have some difference ** if-else: 2.7084999601356687e-06, match-case: 2.6756002625916154e-06, comparisions : 10 if-else: 2.207159996032715e-06, match-case: 1.8606500088935719e-06, comparisions : 100 if-else: 2.139014999556821e-06, match-case: 1.928671001223847e-06, comparisions : 1000 if-else: 2.682303600158775e-06, match-case: 2.626289399631787e-06, comparisions : 1 if-else: 1.1338948200136655e-06, match-case: 7.989683500636602e-07, comparisions : 10 if-else: 7.862168830033624e-07, match-case: 7.83532044995809e-07, comparisions : 100 if-else: 7.918311449997419e-07, match-case: 7.843428884996683e-07, comparisions : 1000 if-else: 7.843063791000168e-07, match-case: 7.842913352399773e-07, comparisions : 1 ** -- ___ Python tracker <https://bugs.python.org/issue44276> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue44276] Replace if-elif-else structure with match-case (PEP634)
Kshitiz Arya added the comment: I have used timeit module. I have also attached the test file with this message -- Added file: https://bugs.python.org/file50081/match_test.py ___ Python tracker <https://bugs.python.org/issue44276> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue44283] Add jump table for certain safe match-case statements
Change by Kshitiz Arya : -- nosy: +Kshitiz17 ___ Python tracker <https://bugs.python.org/issue44283> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue44276] Replace if-elif-else structure with match-case (PEP634)
Kshitiz Arya added the comment: As Brandt shows us, match-case in its current implementation is not significantly different from an if-else ladder in term of performance, though I still maintain that match-case is much more readable than an if-else ladder. I also agree with Karthikeyan and Terry that global replacement is not a good idea, at least not without a significant performance improvement. Therefore I will close this issue. Currently, some people at issue 44283 are working on improving the performance of match-case so fingers are crossed. If there is some significant gain in performance then I will open new issues on case to case basis. -- resolution: -> postponed stage: -> resolved status: open -> closed ___ Python tracker <https://bugs.python.org/issue44276> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com