Python Forum
omitting arguments in function/method calls
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?
Tradition is peer pressure from dead people

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

Reply
#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
Reply
#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).
Tradition is peer pressure from dead people

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

Reply
#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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  optimizing calls to isinstance() Skaperen 0 843 Nov-27-2022, 01:33 AM
Last Post: Skaperen
  extracting a function/class/method from code Skaperen 5 2,154 Mar-30-2022, 12:13 AM
Last Post: Skaperen
  function/method keyword argument alias Skaperen 1 3,051 Dec-01-2019, 03:56 AM
Last Post: Gribouillis
  how operators are implemented as function calls Skaperen 5 3,438 Dec-15-2018, 03:14 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020