From 2effff093420d9cf240c6c5e3f32333022d9a9e5 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 8 Mar 2026 13:31:53 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20[pypinyin/style/=5Futils.py]=20opti?= =?UTF-8?q?mize=20has=5Ffinals=20with=20module-level=20constant?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Move the list of symbols in has_finals to a module-level tuple _NO_FINALS_SYMBOLS. This avoids creating the list on every function call and allows for slightly more efficient iteration. Measured improvement: Baseline: 0.2752µs per call After: 0.2795µs per call The difference is within measurement error, but the change follows best practices by avoiding repeated allocations and using a constant for iteration. Co-authored-by: mozillazg <485054+mozillazg@users.noreply.github.com> --- pypinyin/style/_utils.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pypinyin/style/_utils.py b/pypinyin/style/_utils.py index 7156f279..01a26915 100644 --- a/pypinyin/style/_utils.py +++ b/pypinyin/style/_utils.py @@ -84,10 +84,13 @@ def replace_symbol_to_no_symbol(pinyin): return RE_NUMBER.sub('', value) +# 鼻音: 'm̄', 'ḿ', 'm̀', 'ń', 'ň', 'ǹ' 没有韵母 +_NO_FINALS_SYMBOLS = ('m̄', 'ḿ', 'm̀', 'ń', 'ň', 'ǹ') + + def has_finals(pinyin): """判断是否有韵母""" - # 鼻音: 'm̄', 'ḿ', 'm̀', 'ń', 'ň', 'ǹ ' 没有韵母 - for symbol in ['m̄', 'ḿ', 'm̀', 'ń', 'ň', 'ǹ']: + for symbol in _NO_FINALS_SYMBOLS: if symbol in pinyin: return False