一句话:提出 DeIdiom,将九类 Pythonic 惯用法自动转换为等价非惯用代码,以提升可读性并辅助理解。
Python 社区提倡用 Pythonic 惯用法简洁高效地表达意图,但对 Stack Overflow 上 154 个问题的分析显示用户理解这些惯用法存在多种困难,7,577 个 GitHub 项目又表明其广泛使用。惯用法不仅词法简洁,还会创建变量和函数,难以直接映射回非惯用代码,并可能带来冗余、缺陷和性能下降。作者开发 DeIdiom,自动将惯用代码转换为等价非惯用代码。针对九类惯用法(列表/集合/字典推导、链式比较、真值测试、循环 else、多重赋值、for 多目标、星号等)的 7,572 个实例,测试与审查显示高准确率;20 人用户研究表明生成的解释性非惯用代码能帮助用户正确高效地理解惯用法,并更积极地评价它们。
The Python community strives to design pythonic idioms so that Python users can achieve their intent in a more concise and efficient way. According to our analysis of 154 questions about challenges of understanding pythonic idioms on Stack Overflow, we find that Python users face various challenges in comprehending pythonic idioms. And the usage of pythonic idioms in 7,577 GitHub projects reveals the prevalence of pythonic idioms. By using a statistical sampling method, we find pythonic idioms result in not only lexical conciseness but also the creation of variables and functions, which indicates it is not straightforward to map back to non-idiomatic code. And usage of pythonic idioms may even cause potential negative effects such as code redundancy, bugs and performance degradation. To alleviate such readability issues and negative effects, we develop a transforming tool, DeIdiom, to automatically transform idiomatic code into equivalent non-idiomatic code. We test and review over 7,572 idiomatic code instances of nine pythonic idioms (list/set/dict-comprehension, chain-comparison, truth-value-test, loop-else, assign-multi-targets, for-multi-targets, star), the result shows the high accuracy of DeIdiom. Our user study with 20 participants demonstrates that explanatory non-idiomatic code generated by DeIdiom is useful for Python users to understand pythonic idioms correctly and efficiently, and leads to a more positive appreciation of pythonic idioms.
@inproceedings{paperbot11287,
title = {Hard to Read and Understand Pythonic Idioms? DeIdiom and Explain Them in Non-Idiomatic Equivalent Code},
author = {Zejun Zhang and Zhenchang Xing and Dehai Zhao and Qinghua Lu and Xiwei Xu and Liming Zhu},
booktitle = {ICSE},
year = {2024},
doi = {10.1145/3597503.3639101}
}