python / python/cpython

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

Đang mở
#118,951 1 bình luận 0 reaction 1 người được giao Xem trên GitHub

@ethanfurman đang làm issue này rồi.

Từ ngày 11/5/2024.

docs
Ngôn ngữ chính
Python
Star
77.2k
Fork
36k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

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')

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.