3

I have a piece of code that looks like this:

myfunction(a, b, c)
myfunction(d, e, f)
myfunction(g, h, i)
myfunction(j, k, l)

The number of arguments do not change, but the function has to be called consecutively with different values each time. These values are not automatically generated and are manually inputted. Is there an inline solution to do this without creating a function to call this function? Something like:

myfunction(a, b, c)(d, e f)(g, h, i)(j, k, l)

Any help appreciated. Thanks in advance!

Terrornado
  • 793
  • 3
  • 9
  • 21
  • I don't fully understand what you are wanting to achieve. Do you want to call the function only once with all the possible values? – saud Dec 03 '17 at 06:50
  • Do you want to use the results of calling the function as arguments to the next call of the same function? – salparadise Dec 03 '17 at 07:00
  • You can do `a, b, c = input().split(' ')` in a while True kind of loop and then call class `myfunction(a, b, c)` – Amit Tripathi Dec 03 '17 at 07:02
  • Can you give a sample implementation? No one seems to know exactly what you want to do. What does `myfunction` take and return? – Adam Smith Dec 03 '17 at 07:04
  • @Adam Creates a dictionary key named a, appends value b for number of times c. But not really relevant imo, basically a short way to call same function with different arguments instead of having to manually type it each time. – Terrornado Dec 03 '17 at 07:45
  • @RottenCandy No, call the function multiple times, each time with a different set of values as arguments. – Terrornado Dec 03 '17 at 07:47
  • @salaparadise Nope, The arguments are mutually exclusive and manually inputted, like it says in the question :) I just want a shorter way to call the same function several times with different sets of arguments. – Terrornado Dec 03 '17 at 07:47

5 Answers5

9

Simple, use tuple unpacking

tripples = [('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'h', 'i'), ('j', 'k', 'm')]
for tripple in tripples:
    print(myfunction(*tripple))
Yannic Hamann
  • 4,655
  • 32
  • 50
Xero Smith
  • 1,968
  • 1
  • 14
  • 19
2

I'm surprised that nobody mentioned map

map(func, iter)

It maps each iterable in iter to the func passed in the first argument.

For your use case it should look like

map(myfunction, *zip((a, b, c), (d, e, f), (g, h, i), (j, k, l)))
vpshastry
  • 81
  • 1
  • 7
  • 1
    This doesn't seem to work: The four tuples are interpreted as four parameters, so it fails with `TypeError: myfunction() takes 3 positional arguments but 4 were given`. Better use the same with `map(myfunction, *zip( (a, b, c), ... ))`. – fcdt Aug 05 '20 at 22:12
  • I don't think this works the way you imagine. >>> print(*zip((1,2,3), (4,5,6), (7, 8, 9), (10, 11, 12))) (1, 4, 7, 10) (2, 5, 8, 11) (3, 6, 9, 12) >>> print(*((1,2,3), (4,5,6), (7, 8, 9), (10, 11, 12))) (1, 2, 3) (4, 5, 6) (7, 8, 9) (10, 11, 12) – hughdbrown Nov 25 '20 at 21:09
1

You can misuse list comprehension here

[myfunction(*item) for item in ((a, b, c), (d, e, f), (g, h, i), (j, k, l))]
user2389519
  • 270
  • 2
  • 10
0

Hi I am not sure whether it's the most pythonic way , but if you have arguments defined as in a list the you can call the function in a loop, and remove the n argument from the beginning of the list :

Have a look to the sample code :

def myfunction(x1,x2,x3):
    return x1+x2+x3


arglist = [1,2,3,4,5,6,7,8,9]
for _ in range(3):
    print  myfunction(arglist[_+0],arglist[_+1],arglist[_+2])
    # remove used arguments from the list 
    arglist= arglist[2:] 
coder3521
  • 2,608
  • 1
  • 28
  • 50
  • It's an interesting alternative to simply calling a function multiple times, true, but it seems more convoluted a way to go about than the accepted answer. This is also viable though. – Terrornado Dec 03 '17 at 14:37
  • 2
    @Terrornado : 100% agree with you , even though it servers the purpose , packing unpacking concept is better approach – coder3521 Dec 03 '17 at 14:39
  • 1
    the `_` symbol indicates that the value will never be used. Maybe use `i` instead? – Adam Smith Dec 03 '17 at 21:29
  • @AdamSmith : both has the same meaning , _ also represents a variable , _ or i doesn't matters – coder3521 Dec 04 '17 at 04:55
  • I don't understand why people down vote even if answer is correct , might not be the best but it serves the purpose – coder3521 Dec 04 '17 at 04:56
  • @csharpcoder `_` has no special meaning in Python, but it does to a Python programmer. See https://stackoverflow.com/questions/5893163/what-is-the-purpose-of-the-single-underscore-variable-in-python and please stop using it as you have -- you're making the job of whoever has to maintain your code much harder than it needs to be. `_` or `i` most definitely *does* matter. – Adam Smith Dec 04 '17 at 06:07
  • @csharpcoder and you earned my downvote because I don't believe your answer is useful. Advising indexing a list, even if you used the right loop variable name, over iterating over triples is, well, inadvisable. Note also that your code as-written has a hastily-written typo, and fails on a `NameError`. – Adam Smith Dec 04 '17 at 06:09
  • instead of removing first two, we can use: arglist[3*i], arglist[3*i+1], arglist[3*i+2] – b m gevariya Aug 21 '21 at 16:36
-3

I think what you want to do is something like this...

myfunction(myfunction(d, e, f), myfunction(g, h, i), myfunction(j, k, l))
buddhiv
  • 692
  • 1
  • 7
  • 24