You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The number of arguments a function takes. From words like unary, binary, ternary, etc. This word has the distinction of being composed of two suffixes, "-ary" and "-ity." Addition, for example, takes two arguments, and so it is defined as a binary function or a function with an arity of two. Such a function may sometimes be called "dyadic" by people who prefer Greek roots to Latin. Likewise, a function that takes a variable number of arguments is called "variadic," whereas a binary function must be given two and only two arguments, currying and partial application notwithstanding (see below).
67
+
The number of arguments a function takes. From words like unary, binary, ternary, etc. This word has the distinction of being composed of two suffixes, "-ary" and "-ity". Addition, for example, takes two arguments, and so it is defined as a binary function or a function with an arity of two. Such a function may sometimes be called "dyadic" by people who prefer Greek roots to Latin. Likewise, a function that takes a variable number of arguments is called "variadic," whereas a binary function must be given two and only two arguments, currying and partial application notwithstanding.
68
+
69
+
We can use the `inspect` module to know the arity of a function, see the example below:
The __minimum arity__ is the smallest number of arguments the function expects to work, the __maximum arity__ is the largest number of arguments function can take. Generally, these numbers are different when our function has default parameter values.
85
+
86
+
```python
87
+
>>>from inspect import getfullargspec
88
+
>>>from typing import Any
89
+
90
+
>>>defexample(a: Any, b: Any, c: Any =None) -> None: # mim arity: 2 | max arity: 3
A function has __fixed arity__ when you have to call it with the same number of arguments as the number of its parameters and a function has __variable arity__ when you can call it with variable number of arguments, like functions with default parameters values.
104
+
105
+
```python
106
+
>>>from typing import Any
107
+
108
+
>>>deffixed_arity(a: Any, b: Any) -> None: # we have to call with 2 arguments
109
+
...pass
110
+
111
+
>>>defvariable_arity(a: Any, b: Any =None) -> None: # we can call with 1 or 2 arguments
112
+
...pass
71
113
```
72
114
115
+
#### Definitive Arity and Indefinite Arity
116
+
117
+
When a function can receive a finite number of arguments it has __definitive arity__, otherwise if the function can receive an undefined number of arguments it has __indefinite arity__. We can reproduce the __indefinite arity__ using Python _*args_ and _**kwargs_, see the example below:
118
+
119
+
```python
120
+
>>>from typing import Any
121
+
122
+
>>>defdefinitive_arity(a: Any, b: Any =None) -> None: # we can call just with 1 or 2 arguments
123
+
...pass
124
+
125
+
>>>defindefinite_arity(*args: Any, **kwargs: Any) -> None: # we can call with how many arguments we want
126
+
...pass
127
+
```
128
+
129
+
### Arguments vs Parameters
130
+
131
+
There is a little difference between __arguments__ and __parameters__:
132
+
133
+
*__arguments__: are the values that are passed to a function
134
+
*__parameters__: are the variables in the function definition
135
+
73
136
## Higher-Order Functions (HOF) (TODO)
74
137
75
138
A function which takes a function as an argument and/or returns a function.
0 commit comments