|
| 1 | +def function_name(): |
| 2 | + pass |
| 3 | + |
| 4 | + |
| 5 | +def fn(arg1, arg2): |
| 6 | + return arg1 + arg2 |
| 7 | + |
| 8 | + |
| 9 | +def fn(arg1=0, arg2=0): |
| 10 | + return arg1 + arg2 |
| 11 | + |
| 12 | + |
| 13 | +def fn(arg1: int = 0, arg2: int = 0) -> int: |
| 14 | + return arg1 + arg2 |
| 15 | + |
| 16 | + |
| 17 | +def fn(arg1: int | float, arg2: int | float) -> tuple[float, float]: |
| 18 | + return arg1 + arg2, arg1 * arg2 |
| 19 | + |
| 20 | + |
| 21 | +fn = lambda arg1, arg2: arg1 + arg2 |
| 22 | + |
| 23 | + |
| 24 | +def fn(arg1=0, arg2=0): |
| 25 | + """This function sums two numbers.""" |
| 26 | + return arg1 + arg2 |
| 27 | + |
| 28 | + |
| 29 | +def fn(arg1: int = 0, arg2: int = 0) -> int: |
| 30 | + """ |
| 31 | + This function sums two numbers. |
| 32 | +
|
| 33 | + :param arg1: The first number, default is 0 |
| 34 | + :type arg1: int |
| 35 | + :param arg2: The second number, default is 0 |
| 36 | + :type arg2: int |
| 37 | + :raises TypeError: Both arguments must be integers. |
| 38 | + :return: The sum of the two numbers |
| 39 | + :rtype: int |
| 40 | + """ |
| 41 | + if type(arg1) != int or type(arg2) != int: |
| 42 | + raise TypeError("Both arguments must be integers.") |
| 43 | + return arg1 + arg2 |
| 44 | + |
| 45 | + |
| 46 | +def fn(arg1: int = 0, arg2: int = 0) -> int: |
| 47 | + """ |
| 48 | + This function sums two numbers. |
| 49 | +
|
| 50 | + Args: |
| 51 | + arg1 (int): The first number, default is 0 |
| 52 | + arg2 (int): The second number, default is 0 |
| 53 | +
|
| 54 | + Returns: |
| 55 | + int: The sum of the two numbers |
| 56 | +
|
| 57 | + Raises: |
| 58 | + TypeError: Both arguments must be integers. |
| 59 | + """ |
| 60 | + return arg1 + arg2 |
| 61 | + |
| 62 | + |
| 63 | +def fn(arg1=0, arg2=0): |
| 64 | + return arg1 + arg2 |
| 65 | + |
| 66 | + |
| 67 | +fn(), fn(3), fn(3, 5), fn(arg1=3) |
| 68 | +fn(arg2=5), fn(arg1=3, arg2=5) |
| 69 | + |
| 70 | + |
| 71 | +def fn(arg1=0, arg2=0, *, arg3=1): |
| 72 | + return (arg1 + arg2) * arg3 |
| 73 | + |
| 74 | + |
| 75 | +fn(), fn(3), fn(3, 5), fn(arg1=3), fn(arg2=5) |
| 76 | +fn(arg1=3, arg2=5), fn(3, 5, arg3=2) |
| 77 | +fn(arg1=3, arg2=5, arg3=2) |
| 78 | +fn(arg3=2, arg1=3, arg2=5) |
| 79 | + |
| 80 | + |
| 81 | +fn(3, 5, 2) |
| 82 | +fn(arg3=2, 3, 5) |
| 83 | + |
| 84 | + |
| 85 | +def fn(arg1=0, arg2=0, /, arg3=1, arg4=1, *, arg5=1, arg6=1): |
| 86 | + return (arg1 + arg2) * arg3 / arg4 * arg5**arg6 |
| 87 | + |
| 88 | + |
| 89 | +fn(), fn(3), fn(3, 5), fn(3, 5, 2), fn(3, 5, 2, 4) |
| 90 | +fn(3, 5, arg3=2, arg4=4) |
| 91 | +fn(3, 5, arg3=2, arg4=4, arg5=7, arg6=8) |
| 92 | +fn(3, 5, 2, 4, arg5=7, arg6=8) |
| 93 | + |
| 94 | + |
| 95 | +fn(3, 5, 2, 4, 7, 8) |
| 96 | +fn(arg1=3, arg2=5, arg3=2, arg4=4) |
| 97 | +fn(arg1=3, arg2=5, arg3=2, arg4=4, arg5=7, arg6=8) |
| 98 | + |
| 99 | + |
| 100 | +def fn(*args, **kwargs): |
| 101 | + print(args) # a tuple of positional arguments |
| 102 | + print(kwargs) # a dictionary of keyword arguments |
| 103 | + |
| 104 | + |
| 105 | +def parent_function(): |
| 106 | + def nested_function(): |
| 107 | + print("I'm a nested function.") |
| 108 | + print("I'm a parent function.") |
| 109 | + |
| 110 | +parent_function() |
| 111 | +parent_function.nested_function() |
| 112 | + |
| 113 | + |
| 114 | +def point(x, y): |
| 115 | + def set_x(new_x): |
| 116 | + nonlocal x |
| 117 | + x = new_x |
| 118 | + def set_y(new_y): |
| 119 | + nonlocal y |
| 120 | + y = new_y |
| 121 | + def get(): |
| 122 | + return x, y |
| 123 | + point.set_x = set_x |
| 124 | + point.set_y = set_y |
| 125 | + point.get = get |
| 126 | + return point |
0 commit comments