Python Forum

Full Version: omitting arguments in function/method calls
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
is there a way, in a call to a function that expects 3 arguments, to omit only the 2nd argument?
Only if the second argument has a default.
if it does have a default, then how?
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')
so the caller will need to know the name. that will need to be part of the function's API description.
(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
Note that for a given function foo, the names of the parameters are in foo.__code__.co_varnames.
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).
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
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.
Pages: 1 2