Skip to content

Commit 8595294

Browse files
⚡ [pypinyin/style/_utils.py] optimize has_finals with module-level constant (#366)
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: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> Co-authored-by: mozillazg <485054+mozillazg@users.noreply.github.com>
1 parent fcd68ff commit 8595294

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

pypinyin/style/_utils.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,13 @@ def replace_symbol_to_no_symbol(pinyin):
8484
return RE_NUMBER.sub('', value)
8585

8686

87+
# 鼻音: 'm̄', 'ḿ', 'm̀', 'ń', 'ň', 'ǹ' 没有韵母
88+
_NO_FINALS_SYMBOLS = ('m̄', 'ḿ', 'm̀', 'ń', 'ň', 'ǹ')
89+
90+
8791
def has_finals(pinyin):
8892
"""判断是否有韵母"""
89-
# 鼻音: 'm̄', 'ḿ', 'm̀', 'ń', 'ň', 'ǹ ' 没有韵母
90-
for symbol in ['m̄', 'ḿ', 'm̀', 'ń', 'ň', 'ǹ']:
93+
for symbol in _NO_FINALS_SYMBOLS:
9194
if symbol in pinyin:
9295
return False
9396

0 commit comments

Comments
 (0)