How to use function output as input for another function inside a function

I am currently learning Python and hope someone can help me. I am new to coding, so it would be very helpful if it was explained well.

Let's say I have a function with a name function1that returns:

return (row, column)

Now I am writing another function called say function2. Inside this function, I need to call say:

exampleItem.exampleName(row, column, name)

How to use the output function1, which is a row and column, as arguments rowand columnin the above row from function2?

I really hope this makes sense, as I seriously punished for not writing the question properly before, because I did not understand what was best here.

+4
source share
3 answers

You can use a star *to unpack the result - which is a tuple of the call function function1after placing it as an argument inside another function. This causes the elements returned from function1as positional arguments in the second:

exampleItem.exampleName(*function1(), name)

In Python, 2.xyou can do the same, but you need to provide the remaining positional arguments in the form of a keyword to make it work with *:

exampleItem.exampleName(*function1(), name=name)

this also works on Python 3.x, so you have no portability issues.

, row, column = function1(), :

exampleItem.exampleName(row, column, name)

- . , : -)

+6

Python :

row, column = function1()
exampleName(row, column, name)

(3.5+) , , :

exampleName(*function1(), name)

.: PEP 448 - .

+7

" ", ( ) .

f1(row, column) - , (row, column) ( ):

a = f1(3, 4)
b = exampleItem.exampleName(*a, name="fish")

# Or, as a one-liner:
b = exampleItem.exampleName(*f1(3,4), name="fish")

, tple-unpacked (a , *) , .

, Python 2, - :

x, y = f1(3, 4) # unpacks the length-2 tuple into two variables
b = exampleItem.exampleName(x, y, "fish")

. Python 3 , , name= , Python 3.

: **, , , , .

+2

Source: https://habr.com/ru/post/1662415/


All Articles