python Creating functions that you can remember later

What are you doing with this function, no matter how many weeks later? When I can't remember

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

Example: Simple addition program

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

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.

Commentary

The ʻadditionfunction itself is a program that does a simple addition, and if you setflag to False`, it will only return the result.

Put a description in the function

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`.

Summary

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

python Creating functions that you can remember later
Useful Python built-in functions that you use occasionally
Kalman filter that you can understand
Python | What you can do with Python
Let's create a Python directory structure that you won't regret later
Python functions
Until you can use opencv with python
[Feature-length poem] I don't understand functional languages You can understand it with Python: Part 1 Functions that receive functions are convenient.
Functions that can be used in for statements
You can easily create a GUI with Python
#Python basics (functions)
[Beginner] Python functions
Python Easy-to-use functions
Python basics: functions
"CheckiO" where you can study Python while solving problems
Python knowledge notes that can be used with AtCoder
From a book that programmers can learn ... (Python): Pointer
Makes you think that Python regular expressions are great