Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does multiple function calls works in python 2.7

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

  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.

or does it mean

  1. Call to method split using Work above.
  2. Call to method strip using result from 1 above.
  3. Extracts first element from result of 2 above.
  4. Call to method find using result from 3 above.

Or does Python execute call() functions as they are written?

Thank for the help

like image 594
lufee Avatar asked Sep 19 '26 10:09

lufee


1 Answers

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.

like image 142
Mushroom Man Avatar answered Sep 22 '26 00:09

Mushroom Man