Posts: 4,653
Threads: 1,496
Joined: Sep 2016
is there a way, in a call to a function that expects 3 arguments, to omit only the 2nd argument?
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 4,220
Threads: 97
Joined: Sep 2016
Only if the second argument has a default.
Posts: 4,653
Threads: 1,496
Joined: Sep 2016
if it does have a default, then how?
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 4,220
Threads: 97
Joined: Sep 2016
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')
Posts: 4,653
Threads: 1,496
Joined: Sep 2016
Nov-24-2019, 08:59 AM
(This post was last modified: Nov-24-2019, 09:00 AM by Skaperen.)
so the caller will need to know the name. that will need to be part of the function's API description.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 8,168
Threads: 160
Joined: Sep 2016
Nov-24-2019, 09:12 AM
(This post was last modified: Nov-24-2019, 09:13 AM by buran.)
(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
Posts: 4,220
Threads: 97
Joined: Sep 2016
Note that for a given function foo, the names of the parameters are in foo.__code__.co_varnames.
Posts: 4,653
Threads: 1,496
Joined: Sep 2016
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) .
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 8,168
Threads: 160
Joined: Sep 2016
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
Posts: 4,653
Threads: 1,496
Joined: Sep 2016
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.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
|