Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 omitting arguments in function/method calls
#1
is there a way, in a call to a function that expects 3 arguments, to omit only the 2nd argument?
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#2
Only if the second argument has a default.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#3
if it does have a default, then how?
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#4
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')
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#5
so the caller will need to know the name. that will need to be part of the function's API description.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#6
(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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Quote
#7
Note that for a given function foo, the names of the parameters are in foo.__code__.co_varnames.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#8
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).
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#9
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Quote
#10
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.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  function/method keyword argument alias Skaperen 1 383 Dec-01-2019, 03:56 AM
Last Post: Gribouillis
  how operators are implemented as function calls Skaperen 5 999 Dec-15-2018, 03:14 AM
Last Post: ichabod801

Forum Jump:


Users browsing this thread: 1 Guest(s)