Python Forum
omitting arguments in function/method calls - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: News and Discussions (https://python-forum.io/forum-31.html)
+--- Thread: omitting arguments in function/method calls (/thread-22687.html)

Pages: 1 2


omitting arguments in function/method calls - Skaperen - Nov-22-2019

is there a way, in a call to a function that expects 3 arguments, to omit only the 2nd argument?


RE: omitting arguments in function/method calls - ichabod801 - Nov-22-2019

Only if the second argument has a default.


RE: omitting arguments in function/method calls - Skaperen - Nov-23-2019

if it does have a default, then how?


RE: omitting arguments in function/method calls - ichabod801 - Nov-23-2019

With the parameter's name:

def foo(bar, spam = 'spam', eggs = 'eggs'):
    print("I'll have {} with {} and {}.".format(bar, spam, eggs))
foo('pizza', eggs = 'huevos')



RE: omitting arguments in function/method calls - Skaperen - Nov-24-2019

so the caller will need to know the name. that will need to be part of the function's API description.


RE: omitting arguments in function/method calls - buran - Nov-24-2019

(Nov-24-2019, 08:59 AM)Skaperen Wrote: so the caller will need to know the name. that will need to be part of the function's API description.
well, when you make a function call, you need to know what arguments are. how would you make a call without knowing function arguments? even with only positional arguments


RE: omitting arguments in function/method calls - ichabod801 - Nov-24-2019

Note that for a given function foo, the names of the parameters are in foo.__code__.co_varnames.


RE: omitting arguments in function/method calls - Skaperen - Nov-24-2019

if you are just coding a call to some function referenced by a variable named "fun", and what is documented about it is that there are 3 arguments of related values, and you must omit one and it will calculate the missing one from the others and return that calculated value, the only way to do that is with named parameters?

actually, the function i am thinking of uses 4 values and 2 are to be omitted which it will calculate from the 2 that are given. i just asked about 3 to make it a simpler question. i was originally expecting a positional way to do it, like in another language it would be fun(a,,c).


RE: omitting arguments in function/method calls - buran - Nov-24-2019

your question is too broad, but why not have separate functions? There is nothing to prevent user to supply let's say 3 or 4, not just 2 arguments. you have to account for such possibility


RE: omitting arguments in function/method calls - Skaperen - Nov-24-2019

the 4 argument case could reject any call with only 1 argument provided as "ambiguous" by raising an exception. the 3 or 4 argument cases could be a case of requesting a verification of the values and whether they fit each other, or not by how much. it could return a value indicating how close.

this function, which i have already implemented in C, takes amps, volts, watts, and ohms. given any 2 it calculates the other 2. i'm thinking of using 0.0 as the value meaning omitted.