You can put a comment at the beginning of the function and a description in the argument, so how to use it and how to see it
Sample made: https://github.com/KodairaTomonori/Qiita/blob/master/default_module/syntax/function.py
comennt.py
def addition(num_a: int, num_b: int, flag:bool=True) -> int:
'''
This function is num_a + num_A program that calculates and returns b.
If flag is set to True, the result will be printed for easy viewing.
'''
if flag: print('{} + {} = {}'.format(num_a, num_b, num_a + num_b) )
return num_a + num_b
if __name__ == '__main__':
result = addition(1234, 4321)
print('result:', result)
print('Argument information: ', addition.__annotations__)
print('Function description: ', addition.__doc__)
output.txt
1234 + 4321 = 5555
result: 5555
Argument information: {'num_b': <class 'int'>, 'num_a': <class 'int'>, 'return': <class 'int'>, 'flag': <class 'bool'>}
Function description:
This function is num_a + num_A program that calculates and returns b.
If flag is set to True, the result will be printed for easy viewing.
The ʻadditionfunction itself is a program that does a simple addition, and if you set
flag to
False`, it will only return the result.
If you want to put a description in the argument,
(num_a: int, num_b: int, flag:bool=True) -> int
I will write like a dictionary. Argument name on the left, type on the right (argument: type), and if you want to give a default value, just add it like = True
at the end.
Finally, you can also enter the return type with -> int
.
You can see how to refer to this argument explanation by setting ʻaddition.annotations`.
When you put a description in a function, you can put a string in the next place declared by def
.
You can see how to refer to this description by setting ʻaddition.doc`.
It is important to make the argument name easy to understand even if you look at it later, but it will be easier to understand by including a description, so I will write it without any hassle from now on.
Recommended Posts