python / python/cpython

improvement suggestion for the Coordinate exemple in 3/howto/enum.html

オープン
#118,951 コメント 1 件 リアクション 0 件 担当者 1 名 GitHub で見る

@ethanfurman がすでに取り組んでいます。

2024年5月11日 から。

docs
主要言語
Python
スター
77.2k
フォーク
36k
PR マージ指標
PR 指標を取得中

説明

Documentation

The chapter "When to use __new__() vs. __init__()" starts with a sample class Coordinate(bytes, Enum) and later use 'int' instead of bytes. I would suggest the code here under to improve consistency with bytes inheritance. This suggestion emerged after using the original example raising an exception

>>> Coordinate['PY']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/enum.py", line 1217, in __repr__
    return "<%s.%s: %s>" % (self.__class__.__name__, self._name_, v_repr(self._value_))
                                                                  ^^^^^^^^^^^^^^^^^^^^
TypeError: descriptor '__repr__' requires a 'bytes' object but received a 'int'
class Coordinate(bytes, Enum):
    """
    Coordinate with binary codes that can be indexed by the int code.
    """
    def __new__(cls, value, label, unit):
        obj = bytes.__new__(cls, value)
        obj._value_ = int.from_bytes(value, byteorder='big')
        obj.label = label
        obj.unit = unit
        return obj

    PX = (b'0', 'P.X', 'km')
    PY = (b'1', 'P.Y', 'km')
    VX = (b'2', 'V.X', 'km/s')
    VY = (b'3', 'V.Y', 'km/s')

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。