简体   繁体   中英

Is it possible to show __init_subclass__ docstring on the subclass?

Let's say I have a class that implement __init_subclass__ with two parameters:

class Foo:

    def __init_subclass__(name: str, surname: str):
        """
        name: The name of the user
        surname: The surname of the user
        """
        ...

When I subclass from Foo. I would like to be able to see in the docstring of the subclassed class the description of the two parameter (name, surname). Is there any way to achieve this? This way when a user subclass from Foo, he knows what to put in the parameters of the class

class Bar(Foo, name = 'bar', surname = 'xxx')
    ...

I'm not exactly sure, I understand you correctly, but to add the docs of __init_subclass__ to the subclasses of Foo you can update subclasses __doc__ :

class Foo:
    def __init_subclass__(cls, name: str, surname: str, **kwargs):
        """
        name: The name of the user
        surname: The surname of the user
        """
        cls.__doc__ = Foo.__init_subclass__.__doc__ + cls.__doc__


class Bar(Foo, name='bar', surname='xxx'):
    """Docs of Bar"""

Then

>>> print(Bar.__doc__)

        name: The name of the user
        surname: The surname of the user
        Docs of Bar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM