python / python/cpython

Incomplete documentation of zero preceding width in format specification

未關閉
#131,915 12 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

docs
主要語言
Python
星號
77.2k
分支
36k
PR 合併指標
PR 指標待擷取

描述

The current description of zero preceding width in format is the following:

When no explicit alignment is given, preceding the width field by a zero ('0') character enables sign-aware zero-padding for numeric types, excluding complex. This is equivalent to a fill character of '0' with an alignment type of '='.

This is not complete. Consider the next code snippet:

formats = (
    ' <+#20.0f',
    '<+#20.0f',
    '+#20.0f',

    ' <+#020.0f',
    '<+#020.0f',
    '+#020.0f',
    )

for fmt in formats:
    print(f'Format {fmt!r:>12}: {format(1, fmt)!r}')

It has the following output:

Format  ' <+#20.0f': '+1.                 '
Format   '<+#20.0f': '+1.                 '
Format    '+#20.0f': '                 +1.'
Format ' <+#020.0f': '+1.                 '
Format  '<+#020.0f': '+1.00000000000000000'
Format   '+#020.0f': '+000000000000000001.'

The first three lines do not precede width of 20 with zero, remaining three do. Lines 4 and 5 show that one can precede width with zero even if alignment is specified. Specifically:

  1. If both fill char and alignment are present, then zero is ignored.
  2. If only alignment is present, then zero sets omitted fill char to 0 (omitting fill char by default uses space as demonstrated by line 2).

Those two cases are not covered; line 6 shows the case covered by docs.


Present docs excludes complex numbers, but it is a bit tricky. One can precede width with zero only if both fill char and alignment are present (i.e., when it is ignored), otherwise an exception is raised. This case can be completely omitted as the same exception also happens when fill char itself is zero (f'{1+1j:0<+#20.0f}'). Instead, the fact that complex numbers cannot be zero-padded should be added to the description of fill char.

Linked PRs
  • gh-132149

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

從 issue 中描述的格式規格文件開始,將其中關於零寬度的措辭與提供的 Python 範例進行比較。更新對明確填滿字元和對齊組合的說明,並釐清複數的限制,然後確認文件中的案例是否與所示輸出一致。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
documentation
Issue 類型
文件
難度
2/5
預估耗時
1-3 小時
活躍度
停滯
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。