python / python/cpython

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

未关闭
#118,951 1 条评论 0 个 reaction 已指派 1 人 在 GitHub 查看

@ethanfurman 已经在做这个了。

开始于 2024年5月11日。

docs
主要语言
Python
星标
77.2k
派生
35.9k
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. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。