Reputation: 169
I'm trying to understand multiple function calls in python but kind of confused.
For example if there is two string variables called Work
and Play
and I wrote multiple function calls like:
Work.find(Play.strip().split()[0])
Does this mean like
or does it mean
Or does Python execute call() functions as they are written?
Thank for the help
Upvotes: 3
Views: 153
Reputation: 21453
code methods, much like math, follow the pretty standard convension of "do the stuff inside the brackets first", for example if you did this:
x = sin(1+5)
You would expect the 1+5
to happen first then do sin(6)
where 6
is the result of the inner operation 1+5
. Exact same principle here:
Work.find(Play.strip().split()[0])
First the inner expression Play.strip().split()[0]
is evaluated and then the operation Work.find
is called with the result of the inner expression as the argument.
And again the inner operations are called from left to right much like math, so
Play.strip().split()[0]
will first do Play.strip()
then on the result of that do r.split()
then on the result of that do r[0]
(where r
is the result of the last part)
note that if you really want the absolute difinitive answer then dis
assemble the code:
>>> import dis
>>> dis.dis(compile("Work.find(Play.strip().split()[0])","<example>","eval"))
1 0 LOAD_NAME 0 (Work)
3 LOAD_ATTR 1 (find)
6 LOAD_NAME 2 (Play)
9 LOAD_ATTR 3 (strip)
12 CALL_FUNCTION 0 (0 positional, 0 keyword pair)
15 LOAD_ATTR 4 (split)
18 CALL_FUNCTION 0 (0 positional, 0 keyword pair)
21 LOAD_CONST 0 (0)
24 BINARY_SUBSCR
25 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
28 RETURN_VALUE
Ok maybe that will be more confusing then helpful...
But you can see that the first two CALL_FUNCTION
have no arguments so you can deduct that .strip()
then .split()
were called and the last CALL_FUNCTION
that was called with one positional was Work.Play(<expression>)
.
Upvotes: 1
Reputation: 385870
This:
Work.find(Play.strip().split()[0])
...is does the equivalent of this:
tmp1 = Play.strip()
tmp2 = tmp1.split()
tmp3 = tmp2[0]
Work.find(tmp3)
Python will execute everything inside of ()
before calling the function that ()
is attached to. Within ()
, it evaluates code left-to-right.
Upvotes: 1
Reputation: 400
strip() is called on Play, split() is called on that, and the first returned value from the split call is passed as an argument into the find() call on Work.
Think of the things in the parentheses as an expression that is passed into the call of find(). We could expand this code:
Work.find(Play.strip().split()[0])
To be:
strip_result = Play.strip()
split_result = strip_result.split()
argu = split_result[0]
Work.find(argu)
The first code bit is a lot more compact, but the second is more readable. You should check PEP 8 and your own preference to determine which to use.
Upvotes: 4
Reputation: 6190
The call order is (intermediate variable names added for clarity):
Play.strip()
is called first (x)x.split()
is called (y)y[0]
(z)Work.find(z)
Upvotes: 0
Reputation: 2166
If you have multiple function calls after each other, then you should read them from left to right, and apply the next function call to the previous created object.
So the following from your post is correct:
Does this mean like 1.Call to method strip using Play, 2.Call to method split using result from 1 above, 3.Extracts first element from result of 2 above, 4.Call to method find using result from 3 above.
Upvotes: 1
Reputation: 185
That
Does this mean like 1.Call to method strip using Play, 2.Call to method split using result from 1 above, 3.Extracts first element from result of 2 above, 4.Call to method find using result from 3 above.
Upvotes: 1