IT/Programming

[Python] Docstring 정보 출력

nofence 2024. 4. 10. 14:38

클래스나 메서드, 함수 내부에 Docstring을 정의 후, help 함수 또는 __doc__ 변수를 통해 해당 정보에 대한 출력이 가능하다.

>>> class Test_Class:
...     """ This is Test Class """     
...     def test_method(self):
...         """ This is Test Method """
...         return True
... 

>>> help(Test_Class)
Help on class Test_Class in module __main__:

class Test_Class(builtins.object)
 |  This is Test Class
 |  
 |  Methods defined here:
 |  
 |  test_method(self)
 |      This is Test Method
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)
 
>>> a = Test_Class().__doc__
>>> a
' This is Test Class 


>>> a = Test_Class().test_method.__doc__
>>> a
' This is Test Method '


>>> def test_func():
...     """ This is Test Function """
...     return True
... 

>>> help(test_func)
Help on function test_func in module __main__:

test_func()
    This is Test Function


>>> a = test_func.__doc__
>>> a
' This is Test Function '