In recent versions you can define functions arguments multiple ways. Here is how they can be defined and called:
Copydef fun(a, b, c='foo'):
...
fun(1, 2)
fun(1, 2, 'bar')
fun(1, b=2)
fun(1, b=2, c='bar')
fun(a=1, b=2, c='bar')
where a and b are positional or keyword args and c is an optional positional or keyword arg
or
Copydef fun(a, b, *, c, d='foo'):
...
fun(1, 2, c='bar')
fun(1, b=2, c='bar', d='baz')
fun(a=1, b=2, c='bar', d='baz')
where a and b are positional or keyword args and c is a mandatory kwarg and d is an optional kwarg
or
Copydef fun(a, b, /, c, *, d, e='foo'):
...
fun(1, 2, 3)
fun(1, 2, c=3)
fun(1, 2, c=3, d='bar')
where a and b are positional only args, c is a positional or keyword arg and d is a mandatory kwarg and e is an optional kwarg
In recent versions you can define functions arguments multiple ways. Here is how they can be defined and called:
Copydef fun(a, b, c='foo'):
...
fun(1, 2)
fun(1, 2, 'bar')
fun(1, b=2)
fun(1, b=2, c='bar')
fun(a=1, b=2, c='bar')
where a and b are positional or keyword args and c is an optional positional or keyword arg
or
Copydef fun(a, b, *, c, d='foo'):
...
fun(1, 2, c='bar')
fun(1, b=2, c='bar', d='baz')
fun(a=1, b=2, c='bar', d='baz')
where a and b are positional or keyword args and c is a mandatory kwarg and d is an optional kwarg
or
Copydef fun(a, b, /, c, *, d, e='foo'):
...
fun(1, 2, 3)
fun(1, 2, c=3)
fun(1, 2, c=3, d='bar')
where a and b are positional only args, c is a positional or keyword arg and d is a mandatory kwarg and e is an optional kwarg
The * prefix means "arbitrary number of positional parameters", and parameters prefixed by it can be declared without default value.
The word 'some' relates to that reason, you don't have to provide default values for all optional arguments.
python - How do I define a function with optional arguments? - Stack Overflow
Pydantic: Difference between typing.Optional and default value
Function argument with a default value is not like Optional
Adding optional arguments to a function
Videos
Just use the *args parameter, which allows you to pass as many arguments as you want after your a,b,c. You would have to add some logic to map args->c,d,e,f but its a "way" of overloading.
Copydef myfunc(a,b, *args, **kwargs):
for ar in args:
print ar
myfunc(a,b,c,d,e,f)
And it will print values of c,d,e,f
Similarly you could use the kwargs argument and then you could name your parameters.
Copydef myfunc(a,b, *args, **kwargs):
c = kwargs.get('c', None)
d = kwargs.get('d', None)
#etc
myfunc(a,b, c='nick', d='dog', ...)
And then kwargs would have a dictionary of all the parameters that are key valued after a,b
Try calling it like: obj.some_function( '1', 2, '3', g="foo", h="bar" ). After the required positional arguments, you can specify specific optional arguments by name.
I am new to Pydantic. In one of the fastapi tutorials for validating the request body a pydantic model was declared. In one of the models for optional parameters typing.Optional was used.
# Optional approach id: Optional[int] = None # default value approach id: int = None
What is the difference between the above two approaches ?
Why a function default argument is not a syntactic sugar for Optional so one can programmatically pass None to get the default value ? What's the benefit ?
How should we do wrappers functions without copying the default values of the wrapped function ?