Python-Ref > Basic usage > Functions > Understanding combined function calls
 
 

<-^^
Klíčová slova
Moduly
Knihovní funkce

Understanding combined function calls

What happens when more functions nested or otherwise combined?
Expand/Shrink
Zdroj: (func9-1.py)
  1   ## al the following code samples are equal
  2   
  3   # code A
  4   a = 1
  5   b = 2
  6   c = 3
  7   x = a + b
  8   y = x / c
  9   print y
 10   
 11   # code B
 12   a = 1
 13   b = 2
 14   c = 3
 15   y = (a + b) / c
 16   print y
 17   
 18   # code C
 19   y = (1 + 2) / 3
 20   print y
stdout:
1
1
1
Doba běhu: 20.6 ms
Expand/Shrink
Zdroj: (func9-2.py)
  1   import math
  2   
  3   ## the following code samples are equal
  4   
  5   # code A
  6   a = 1
  7   x = math.sin( a)
  8   y = math.cos( x)
  9   z = y ** 2
 10   print z
 11   
 12   # code B
 13   a = 1
 14   y = math.cos( math.sin( a))
 15   z = y ** 2
 16   print z
 17   
 18   # code C
 19   a = 1
 20   z = math.cos( math.sin( a)) ** 2
 21   print z
stdout:
0.444044639366
0.444044639366
0.444044639366
Doba běhu: 20.7 ms
Expand/Shrink
Zdroj: (func9-3.py)
  1   ## the following code samples are equal
  2   
  3   # code A
  4   a = "  Some text with more than one word and whitespace   "
  5   x = a.strip()
  6   y = x.split()
  7   z = y[0]
  8   print z
  9   
 10   # code B
 11   a = "  Some text with more than one word and whitespace   "
 12   y = a.strip().split()
 13   z = y[0]
 14   print z
 15   
 16   # code C
 17   a = "  Some text with more than one word and whitespace   "
 18   z = a.strip().split()[0]
 19   print z
 20   
 21   # .. and even code D
 22   z = "  Some text with more than one word and whitespace   ".strip().split()[0]
 23   print z
stdout:
Some
Some
Some
Some
Doba běhu: 20.0 ms