Hướng dẫn python document class attributes - thuộc tính lớp tài liệu python

Các tài liệu theo phong cách Napoleon như chúng được mô tả trong các tài liệu Sphinx (xem lớp ExampleError để họ đảm nhận nó) chạm rõ ràng về trường hợp của bạn:

Nội dung chính

  • GitHub: 82582messages: + msg405772
  • 2021-12-29 & NBSP; 12: 47: 39messages: + msg354507
  • Nosy: + EGEBESMESSAGES: + MSG409304
  • __ init __ có một tài liệu?

Phương pháp __init__ có thể được ghi lại trong tài liệu cấp lớp hoặc như một tài liệu trên chính phương thức __init__.

Và nếu bạn không muốn hành vi này, bạn phải nói rõ ràng cho Sphinx rằng tài liệu xây dựng và tài liệu lớp không giống nhau.

Có nghĩa là, bạn chỉ có thể dán thông tin cấu trúc của bạn vào phần thân của tài liệu lớp.


Trong trường hợp bạn xây dựng tài liệu từ các tài liệu của mình, đây là những hạt có thể đạt được:

1) Tối thiểu trần:

@dataclass
class TestClass:
    """This is a test class for dataclasses.

    This is the body of the docstring description.
    """
    var_int: int
    var_str: str

Hướng dẫn python document class attributes - thuộc tính lớp tài liệu python

2) Tham số trình xây dựng bổ sung Mô tả:

@dataclass
class TestClass:
    """This is a test class for dataclasses.

    This is the body of the docstring description.

    Args:
        var_int (int): An integer.
        var_str (str): A string.

    """
    var_int: int
    var_str: str

3) Mô tả thuộc tính bổ sung:

@dataclass
class TestClass:
    """This is a test class for dataclasses.

    This is the body of the docstring description.

    Attributes:
        var_int (int): An integer.
        var_str (str): A string.

    """
    var_int: int
    var_str: str

Các mô tả tham số và thuộc tính tất nhiên cũng có thể được kết hợp, nhưng vì các dữ liệu nên được ánh xạ thẳng về phía trước, tôi không thấy lý do để làm như vậy.

Theo tôi, 1) sẽ làm cho các dữ liệu nhỏ hoặc đơn giản - nó đã bao gồm chữ ký của hàm tạo với các loại tương ứng của chúng, rất nhiều cho một DataClass. Nếu bạn muốn nói thêm về mỗi thuộc tính, 3) sẽ phục vụ tốt nhất.1) would do for small or simple dataclasses -- it already includes the constructor signature with their respective types, which is plenty for a dataclass. If you want to say more about each attribute, 3) would serve best. 1) would do for small or simple dataclasses -- it already includes the constructor signature with their respective types, which is plenty for a dataclass. If you want to say more about each attribute, 3) would serve best. 1) would do for small or simple dataclasses -- it already includes the constructor signature with their respective types, which is plenty for a dataclass. If you want to say more about each attribute, 3) would serve best.

Số phát hành38401

Được tạo ra vào năm 2019-10-08 00:24 bởi John Parejko2, đã thay đổi lần cuối năm 2022-04-11 14:59 bởi Quản trị viên. Vấn đề giờ đã kết thúc.2019-10-08 00:24 by John Parejko2, last changed 2022-04-11 14:59 by admin. This issue is now closed.2019-10-08 00:24 by John Parejko2, last changed 2022-04-11 14:59 by admin. This issue is now closed.2019-10-08 00:24 by John Parejko2, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Tin nhắn (5)
MSG354154 - (Xem)Tác giả: John Parejko (John Parejko2)Ngày: 2019-10-08 00:24
Dataclasses provide a very straightforward way to specify structured data. One can trivally document a dataclass's attributes via triple-quoted attribute docstrings per PEP 257. However, those docstrings are not accessible to pydoc, so they are lost to users of the dataclass.

For example, the attribute docstrings in the below dataclass should be available when looking at `help(SpectralData)`, but that help command does not show the docstrings.

```
@dataclasses.dataclass
class SpectralData:
    """Class to hold data and metadata from a fiber spectrograph."""
    wavelength: astropy.units.Quantity
    """The wavelength array produced by the instrument."""
    spectrum: np.ndarray
    """The flux array in instrumental units."""
    duration: float
    """The duration of the exposure in seconds."""
```
MSG354192 - (Xem)Tác giả: Eric V. Smith (Eric.Smith) ****Ngày: 2019-10-08 11:56
Note that those strings are not docstrings, per se. They're just strings, and aren't available in the class definition for the dataclass decorator to see. It's really no different from:

class X:
    x: int
    3

That 3 just gets evaluated and thrown away, like the strings in your example. To change this is beyond the scope of dataclasses, and would need to be a language change. I can't think of a good way to attach the string to the annotation before it (I'm assuming this would only work with annotations, but maybe you have other ideas). You might want to bring this up on python-ideas to get some more feedback.
MSG354507 - (Xem)Tác giả: Tal Einat (Taleinat) ****Ngày: 2019-10-11 22:09
Thanks for this suggestion John!

Unfortunately, only the first string literal in the example is a doc-string, and the rest indeed are not. (PEP-258 suggested adding such doc-strings, but it was rejected.)

There is currently no standard way to attach a doc-string to a field in dataclasses, nor in attrs on which dataclasses was mostly based. However, both libraries support custom field meta-data, using which one could add such per-field documentation. So if one really needs something like this, it is possible to work up a custom solution.

Eric is correct that this is far from a straightforward suggestion, which would require a significant language change to implement. Due to the depth of the change required, it would need to be discussed on Python-ideas mailing list first. Please bring this up there if you'd like to pursue this further.

I'm closing this as "rejected" for now since I believe the likelihood of pursuing this to be very small, but this can be re-opened later if further discussion warrants it.
MSG405772 - (Xem)Tác giả: Václav Brožík (Pabouk)Ngày: 2021-11-05 09:19
Note that John Parejko is right that  the active PEP 257 clearly specifies attribute docstrings. Here is the excerpt:

String literals occurring immediately after a simple assignment at the top level of a module, class, or __init__ method are called "attribute docstrings".

They are being supported by various software like Sphinx, vim, VS Codes's Pylance.
MSG409304 - (Xem)Tác giả: Erik Gebeshuber (EGEBES)Ngày: 2021-12-29 12:47
There is some confusion in the answers that I want to clear up:

"Attribute docstrings" were suggested in PEP 224 in August 2000 and rejected March 2001: https://www.python.org/dev/peps/pep-0224/

taleinat mentioned already PEP 258 from May 2001, which also contained "attribute docstrings" and was as well rejected.

At the same time - May 2001 - the well-known PEP 257 about docstring conventions came up and was accepted. It also mentions "attribute docstrings", but in a kind of restricted way (highlighting *** by me):

> String literals occurring elsewhere in Python code ***may also act as documentation***. They are ***not recognized*** by the Python bytecode compiler and are ***not accessible*** as runtime object attributes (i.e. not assigned to __doc__), ...
> Please see PEP 258, "Docutils Design Specification" [2], for a detailed description of attribute and additional docstrings.

The reference to the rejected PEP 258 does in my opinion not make the concept of "attribute docstrings" invalid, but indeed the restrictive wording (highlighted *** in the quote) defines their status, and it helps to explain the situation:

* Attribute docstrings are not supported by Python itself (no __doc__ etc.) -> that's why it is hard to add support for dataclass documentation in `help` as John Parejko suggested.

* It is totally fine to use attribute docstrings, since PEP 257 explicitly mentions them. Various tools support them.

* There is - as far as I can see it - no clear notion to differentiate between "official" docstrings and "inofficial" ones (attribute docstrings, additional docstrings). All of them are docstrings in the broader sense, though most times only the "official" ones are meant, and many don't even know about the "inofficial" ones.
Lịch sử
NgàyNgười sử dụngHoạt độngArgs
2022-04-11 & NBSP; 14: 59: 21 quản trị viên bộ GitHub: 82582
2021-12-29 & NBSP; 12: 47: 39 EGEBES bộ GitHub: 82582
messages: + msg409304
2021-12-29 & NBSP; 12: 47: 39 EGEBES bộ GitHub: 82582
messages: + msg405772
2021-12-29 & NBSP; 12: 47: 39 EGEBES bộ GitHub: 82582
2021-12-29 & NBSP; 12: 47: 39 EGEBES bộ GitHub: 82582

2021-12-29 & NBSP; 12: 47: 39
messages: + msg354507

EGEBES
stage: resolved

GitHub: 82582 messages: + msg409304GitHub: 82582 messages: + msg405772bộ GitHub: 82582
2021-12-29 & NBSP; 12: 47: 39 EGEBES bộ GitHub: 82582
2021-12-29 & NBSP; 12: 47: 39 EGEBES GitHub: 82582 messages: + msg409304

GitHub: 82582messages: + msg405772

GitHub: 82582 messages: + msg405772you should have proper docstrings for the __init__ method.

2021-12-29 & NBSP; 12: 47: 39messages: + msg354507

2021-12-29 & NBSP; 12: 47: 39 messages: + msg354507a string literal that occurs as the first statement in a module, function, class, or method definition. Such a docstring becomes the __doc__ special attribute of that object. All modules should normally have docstrings, and all functions and classes exported by a module should also have docstrings.

Nosy: + EGEBESMESSAGES: + MSG409304

EGEBES stage: resolvedjust after initialization. In other words, it is called after the object receives values for its fields, such as name , continent , population , and official_lang .

__ init __ có một tài liệu?

GitHub: 82582messages: + msg409304 A brief description of the module and its purpose. A list of any classes, exception, functions, and any other objects exported by the module.