Exceptions Announcements Exceptions Today's Topic: Handling Errors - - PowerPoint PPT Presentation

exceptions announcements exceptions today s topic
SMART_READER_LITE
LIVE PREVIEW

Exceptions Announcements Exceptions Today's Topic: Handling Errors - - PowerPoint PPT Presentation

Exceptions Announcements Exceptions Today's Topic: Handling Errors 4 Today's Topic: Handling Errors Sometimes, computer programs behave in non-standard ways 4 Today's Topic: Handling Errors Sometimes, computer programs behave in


slide-1
SLIDE 1

Exceptions

slide-2
SLIDE 2

Announcements

slide-3
SLIDE 3

Exceptions

slide-4
SLIDE 4

Today's Topic: Handling Errors

4

slide-5
SLIDE 5

Today's Topic: Handling Errors

Sometimes, computer programs behave in non-standard ways

4

slide-6
SLIDE 6

Today's Topic: Handling Errors

Sometimes, computer programs behave in non-standard ways

  • A function receives an argument value of an improper type

4

slide-7
SLIDE 7

Today's Topic: Handling Errors

Sometimes, computer programs behave in non-standard ways

  • A function receives an argument value of an improper type
  • Some resource (such as a file) is not available

4

slide-8
SLIDE 8

Today's Topic: Handling Errors

Sometimes, computer programs behave in non-standard ways

  • A function receives an argument value of an improper type
  • Some resource (such as a file) is not available
  • A network connection is lost in the middle of data transmission

4

slide-9
SLIDE 9

Today's Topic: Handling Errors

Sometimes, computer programs behave in non-standard ways

  • A function receives an argument value of an improper type
  • Some resource (such as a file) is not available
  • A network connection is lost in the middle of data transmission

Grace Hopper's Notebook, 1947, Moth found in a Mark II Computer

4

slide-10
SLIDE 10

Exceptions

5

slide-11
SLIDE 11

Exceptions

A built-in mechanism in a programming language to declare and respond to exceptional conditions

5

slide-12
SLIDE 12

Exceptions

A built-in mechanism in a programming language to declare and respond to exceptional conditions Python raises an exception whenever an error occurs

5

slide-13
SLIDE 13

Exceptions

A built-in mechanism in a programming language to declare and respond to exceptional conditions Python raises an exception whenever an error occurs Exceptions can be handled by the program, preventing the interpreter from halting

5

slide-14
SLIDE 14

Exceptions

A built-in mechanism in a programming language to declare and respond to exceptional conditions Python raises an exception whenever an error occurs Exceptions can be handled by the program, preventing the interpreter from halting Unhandled exceptions will cause Python to halt execution and print a stack trace

5

slide-15
SLIDE 15

Exceptions

A built-in mechanism in a programming language to declare and respond to exceptional conditions Python raises an exception whenever an error occurs Exceptions can be handled by the program, preventing the interpreter from halting Unhandled exceptions will cause Python to halt execution and print a stack trace Mastering exceptions:

5

slide-16
SLIDE 16

Exceptions

A built-in mechanism in a programming language to declare and respond to exceptional conditions Python raises an exception whenever an error occurs Exceptions can be handled by the program, preventing the interpreter from halting Unhandled exceptions will cause Python to halt execution and print a stack trace Exceptions are objects! They have classes with constructors. Mastering exceptions:

5

slide-17
SLIDE 17

Exceptions

A built-in mechanism in a programming language to declare and respond to exceptional conditions Python raises an exception whenever an error occurs Exceptions can be handled by the program, preventing the interpreter from halting Unhandled exceptions will cause Python to halt execution and print a stack trace Exceptions are objects! They have classes with constructors. They enable non-local continuation of control Mastering exceptions:

5

slide-18
SLIDE 18

Exceptions

A built-in mechanism in a programming language to declare and respond to exceptional conditions Python raises an exception whenever an error occurs Exceptions can be handled by the program, preventing the interpreter from halting Unhandled exceptions will cause Python to halt execution and print a stack trace Exceptions are objects! They have classes with constructors. They enable non-local continuation of control If f calls g and g calls h, exceptions can shift control from h to f without waiting for g to return. Mastering exceptions:

5

slide-19
SLIDE 19

Exceptions

A built-in mechanism in a programming language to declare and respond to exceptional conditions Python raises an exception whenever an error occurs Exceptions can be handled by the program, preventing the interpreter from halting Unhandled exceptions will cause Python to halt execution and print a stack trace Exceptions are objects! They have classes with constructors. They enable non-local continuation of control If f calls g and g calls h, exceptions can shift control from h to f without waiting for g to return. (Exception handling tends to be slow.) Mastering exceptions:

5

slide-20
SLIDE 20

Raising Exceptions

slide-21
SLIDE 21

Assert Statements

Assert statements raise an exception of type AssertionError

7

slide-22
SLIDE 22

Assert Statements

Assert statements raise an exception of type AssertionError assert <expression>, <string>

7

slide-23
SLIDE 23

Assert Statements

Assert statements raise an exception of type AssertionError assert <expression>, <string> Assertions are designed to be used liberally. They can be ignored to increase efficiency by running Python with the "-O" flag; "O" stands for optimized

7

slide-24
SLIDE 24

Assert Statements

Assert statements raise an exception of type AssertionError assert <expression>, <string> Assertions are designed to be used liberally. They can be ignored to increase efficiency by running Python with the "-O" flag; "O" stands for optimized python3 -O

7

slide-25
SLIDE 25

Assert Statements

Assert statements raise an exception of type AssertionError assert <expression>, <string> Assertions are designed to be used liberally. They can be ignored to increase efficiency by running Python with the "-O" flag; "O" stands for optimized python3 -O Whether assertions are enabled is governed by a bool __debug__

7

slide-26
SLIDE 26

Assert Statements

Assert statements raise an exception of type AssertionError assert <expression>, <string> Assertions are designed to be used liberally. They can be ignored to increase efficiency by running Python with the "-O" flag; "O" stands for optimized python3 -O Whether assertions are enabled is governed by a bool __debug__

7

(Demo)

slide-27
SLIDE 27

Raise Statements

8

slide-28
SLIDE 28

Raise Statements

Exceptions are raised with a raise statement

8

slide-29
SLIDE 29

Raise Statements

Exceptions are raised with a raise statement raise <expression>

8

slide-30
SLIDE 30

Raise Statements

Exceptions are raised with a raise statement raise <expression> <expression> must evaluate to a subclass of BaseException or an instance of one

8

slide-31
SLIDE 31

Raise Statements

Exceptions are raised with a raise statement raise <expression> <expression> must evaluate to a subclass of BaseException or an instance of one Exceptions are constructed like any other object. E.g., TypeError('Bad argument!')

8

slide-32
SLIDE 32

Raise Statements

Exceptions are raised with a raise statement raise <expression> <expression> must evaluate to a subclass of BaseException or an instance of one Exceptions are constructed like any other object. E.g., TypeError('Bad argument!') TypeError -- A function was passed the wrong number/type of argument

8

slide-33
SLIDE 33

Raise Statements

Exceptions are raised with a raise statement raise <expression> <expression> must evaluate to a subclass of BaseException or an instance of one Exceptions are constructed like any other object. E.g., TypeError('Bad argument!') TypeError -- A function was passed the wrong number/type of argument NameError -- A name wasn't found

8

slide-34
SLIDE 34

Raise Statements

Exceptions are raised with a raise statement raise <expression> <expression> must evaluate to a subclass of BaseException or an instance of one Exceptions are constructed like any other object. E.g., TypeError('Bad argument!') TypeError -- A function was passed the wrong number/type of argument NameError -- A name wasn't found KeyError -- A key wasn't found in a dictionary

8

slide-35
SLIDE 35

Raise Statements

Exceptions are raised with a raise statement raise <expression> <expression> must evaluate to a subclass of BaseException or an instance of one Exceptions are constructed like any other object. E.g., TypeError('Bad argument!') TypeError -- A function was passed the wrong number/type of argument NameError -- A name wasn't found KeyError -- A key wasn't found in a dictionary RecursionError -- Too many recursive calls

8

slide-36
SLIDE 36

Raise Statements

Exceptions are raised with a raise statement raise <expression> <expression> must evaluate to a subclass of BaseException or an instance of one Exceptions are constructed like any other object. E.g., TypeError('Bad argument!') TypeError -- A function was passed the wrong number/type of argument NameError -- A name wasn't found KeyError -- A key wasn't found in a dictionary RecursionError -- Too many recursive calls

8

(Demo)

slide-37
SLIDE 37

Try Statements

slide-38
SLIDE 38

Try Statements

10

slide-39
SLIDE 39

Try Statements

Try statements handle exceptions

10

slide-40
SLIDE 40

Try Statements

Try statements handle exceptions try: <try suite> except <exception class> as <name>: <except suite> ...

10

slide-41
SLIDE 41

Try Statements

Try statements handle exceptions try: <try suite> except <exception class> as <name>: <except suite> ...

Execution rule:

10

slide-42
SLIDE 42

Try Statements

Try statements handle exceptions try: <try suite> except <exception class> as <name>: <except suite> ...

Execution rule: The <try suite> is executed first

10

slide-43
SLIDE 43

Try Statements

Try statements handle exceptions try: <try suite> except <exception class> as <name>: <except suite> ...

Execution rule: The <try suite> is executed first If, during the course of executing the <try suite>, an exception is raised that is not handled otherwise, and

10

slide-44
SLIDE 44

Try Statements

Try statements handle exceptions try: <try suite> except <exception class> as <name>: <except suite> ...

Execution rule: The <try suite> is executed first If, during the course of executing the <try suite>, an exception is raised that is not handled otherwise, and If the class of the exception inherits from <exception class>, then

10

slide-45
SLIDE 45

Try Statements

Try statements handle exceptions try: <try suite> except <exception class> as <name>: <except suite> ...

Execution rule: The <try suite> is executed first If, during the course of executing the <try suite>, an exception is raised that is not handled otherwise, and If the class of the exception inherits from <exception class>, then The <except suite> is executed, with <name> bound to the exception

10

slide-46
SLIDE 46

Handling Exceptions

11

slide-47
SLIDE 47

Handling Exceptions

Exception handling can prevent a program from terminating

11

slide-48
SLIDE 48

Handling Exceptions

Exception handling can prevent a program from terminating >>> try:

11

slide-49
SLIDE 49

Handling Exceptions

Exception handling can prevent a program from terminating >>> try: x = 1/0

11

slide-50
SLIDE 50

Handling Exceptions

Exception handling can prevent a program from terminating >>> try: x = 1/0 except ZeroDivisionError as e:

11

slide-51
SLIDE 51

Handling Exceptions

Exception handling can prevent a program from terminating >>> try: x = 1/0 except ZeroDivisionError as e: print('handling a', type(e))

11

slide-52
SLIDE 52

Handling Exceptions

Exception handling can prevent a program from terminating >>> try: x = 1/0 except ZeroDivisionError as e: print('handling a', type(e)) x = 0

11

slide-53
SLIDE 53

Handling Exceptions

Exception handling can prevent a program from terminating >>> try: x = 1/0 except ZeroDivisionError as e: print('handling a', type(e)) x = 0 handling a <class 'ZeroDivisionError'>

11

slide-54
SLIDE 54

Handling Exceptions

Exception handling can prevent a program from terminating >>> try: x = 1/0 except ZeroDivisionError as e: print('handling a', type(e)) x = 0 handling a <class 'ZeroDivisionError'> >>> x

11

slide-55
SLIDE 55

Handling Exceptions

Exception handling can prevent a program from terminating >>> try: x = 1/0 except ZeroDivisionError as e: print('handling a', type(e)) x = 0 handling a <class 'ZeroDivisionError'> >>> x Multiple try statements: Control jumps to the except suite of the most recent try statement that handles that type of exception

11

slide-56
SLIDE 56

Handling Exceptions

Exception handling can prevent a program from terminating >>> try: x = 1/0 except ZeroDivisionError as e: print('handling a', type(e)) x = 0 handling a <class 'ZeroDivisionError'> >>> x Multiple try statements: Control jumps to the except suite of the most recent try statement that handles that type of exception

11

(Demo)

slide-57
SLIDE 57

WWPD: What Would Python Display?

How will the Python interpreter respond?

12

slide-58
SLIDE 58

WWPD: What Would Python Display?

How will the Python interpreter respond?

12

slide-59
SLIDE 59

WWPD: What Would Python Display?

How will the Python interpreter respond? def invert(x): inverse = 1/x # Raises a ZeroDivisionError if x is 0 print('Never printed if x is 0') return inverse def invert_safe(x): try: return invert(x) except ZeroDivisionError as e: return str(e)

12

slide-60
SLIDE 60

WWPD: What Would Python Display?

How will the Python interpreter respond? >>> invert_safe(1/0) def invert(x): inverse = 1/x # Raises a ZeroDivisionError if x is 0 print('Never printed if x is 0') return inverse def invert_safe(x): try: return invert(x) except ZeroDivisionError as e: return str(e)

12

slide-61
SLIDE 61

WWPD: What Would Python Display?

How will the Python interpreter respond? >>> invert_safe(1/0) >>> try: def invert(x): inverse = 1/x # Raises a ZeroDivisionError if x is 0 print('Never printed if x is 0') return inverse def invert_safe(x): try: return invert(x) except ZeroDivisionError as e: return str(e)

12

slide-62
SLIDE 62

WWPD: What Would Python Display?

How will the Python interpreter respond? >>> invert_safe(1/0) >>> try: ... invert_safe(0) def invert(x): inverse = 1/x # Raises a ZeroDivisionError if x is 0 print('Never printed if x is 0') return inverse def invert_safe(x): try: return invert(x) except ZeroDivisionError as e: return str(e)

12

slide-63
SLIDE 63

WWPD: What Would Python Display?

How will the Python interpreter respond? >>> invert_safe(1/0) >>> try: ... invert_safe(0) ... except ZeroDivisionError as e: def invert(x): inverse = 1/x # Raises a ZeroDivisionError if x is 0 print('Never printed if x is 0') return inverse def invert_safe(x): try: return invert(x) except ZeroDivisionError as e: return str(e)

12

slide-64
SLIDE 64

WWPD: What Would Python Display?

How will the Python interpreter respond? >>> invert_safe(1/0) >>> try: ... invert_safe(0) ... except ZeroDivisionError as e: ... print('Hello!') def invert(x): inverse = 1/x # Raises a ZeroDivisionError if x is 0 print('Never printed if x is 0') return inverse def invert_safe(x): try: return invert(x) except ZeroDivisionError as e: return str(e)

12

slide-65
SLIDE 65

WWPD: What Would Python Display?

How will the Python interpreter respond? >>> invert_safe(1/0) >>> try: ... invert_safe(0) ... except ZeroDivisionError as e: ... print('Hello!') >>> inverrrrt_safe(1/0) def invert(x): inverse = 1/x # Raises a ZeroDivisionError if x is 0 print('Never printed if x is 0') return inverse def invert_safe(x): try: return invert(x) except ZeroDivisionError as e: return str(e)

12

slide-66
SLIDE 66

Example: Reduce

slide-67
SLIDE 67

Reducing a Sequence to a Value

14

slide-68
SLIDE 68

Reducing a Sequence to a Value

def reduce(f, s, initial): """Combine elements of s pairwise using f, starting with initial. E.g., reduce(mul, [2, 4, 8], 1) is equivalent to mul(mul(mul(1, 2), 4), 8). >>> reduce(mul, [2, 4, 8], 1) 64 """

14

slide-69
SLIDE 69

f is ... a two-argument function

Reducing a Sequence to a Value

def reduce(f, s, initial): """Combine elements of s pairwise using f, starting with initial. E.g., reduce(mul, [2, 4, 8], 1) is equivalent to mul(mul(mul(1, 2), 4), 8). >>> reduce(mul, [2, 4, 8], 1) 64 """

14

slide-70
SLIDE 70

f is ... a two-argument function s is ... a sequence of values that can be the second argument

Reducing a Sequence to a Value

def reduce(f, s, initial): """Combine elements of s pairwise using f, starting with initial. E.g., reduce(mul, [2, 4, 8], 1) is equivalent to mul(mul(mul(1, 2), 4), 8). >>> reduce(mul, [2, 4, 8], 1) 64 """

14

slide-71
SLIDE 71

f is ... a two-argument function s is ... a sequence of values that can be the second argument initial is ... a value that can be the first argument

Reducing a Sequence to a Value

def reduce(f, s, initial): """Combine elements of s pairwise using f, starting with initial. E.g., reduce(mul, [2, 4, 8], 1) is equivalent to mul(mul(mul(1, 2), 4), 8). >>> reduce(mul, [2, 4, 8], 1) 64 """

14

slide-72
SLIDE 72

f is ... a two-argument function s is ... a sequence of values that can be the second argument initial is ... a value that can be the first argument

Reducing a Sequence to a Value

def reduce(f, s, initial): """Combine elements of s pairwise using f, starting with initial. E.g., reduce(mul, [2, 4, 8], 1) is equivalent to mul(mul(mul(1, 2), 4), 8). >>> reduce(mul, [2, 4, 8], 1) 64 """

14

reduce(pow, [1, 2, 3, 4], 2)

slide-73
SLIDE 73

f is ... a two-argument function s is ... a sequence of values that can be the second argument initial is ... a value that can be the first argument

Reducing a Sequence to a Value

def reduce(f, s, initial): """Combine elements of s pairwise using f, starting with initial. E.g., reduce(mul, [2, 4, 8], 1) is equivalent to mul(mul(mul(1, 2), 4), 8). >>> reduce(mul, [2, 4, 8], 1) 64 """

14

reduce(pow, [1, 2, 3, 4], 2) pow

slide-74
SLIDE 74

f is ... a two-argument function s is ... a sequence of values that can be the second argument initial is ... a value that can be the first argument

Reducing a Sequence to a Value

def reduce(f, s, initial): """Combine elements of s pairwise using f, starting with initial. E.g., reduce(mul, [2, 4, 8], 1) is equivalent to mul(mul(mul(1, 2), 4), 8). >>> reduce(mul, [2, 4, 8], 1) 64 """

14

reduce(pow, [1, 2, 3, 4], 2) pow 2

slide-75
SLIDE 75

f is ... a two-argument function s is ... a sequence of values that can be the second argument initial is ... a value that can be the first argument

Reducing a Sequence to a Value

def reduce(f, s, initial): """Combine elements of s pairwise using f, starting with initial. E.g., reduce(mul, [2, 4, 8], 1) is equivalent to mul(mul(mul(1, 2), 4), 8). >>> reduce(mul, [2, 4, 8], 1) 64 """

14

reduce(pow, [1, 2, 3, 4], 2) pow 2

[ [

slide-76
SLIDE 76

f is ... a two-argument function s is ... a sequence of values that can be the second argument initial is ... a value that can be the first argument

Reducing a Sequence to a Value

def reduce(f, s, initial): """Combine elements of s pairwise using f, starting with initial. E.g., reduce(mul, [2, 4, 8], 1) is equivalent to mul(mul(mul(1, 2), 4), 8). >>> reduce(mul, [2, 4, 8], 1) 64 """

14

reduce(pow, [1, 2, 3, 4], 2) pow 2 1

[ [

slide-77
SLIDE 77

f is ... a two-argument function s is ... a sequence of values that can be the second argument initial is ... a value that can be the first argument

Reducing a Sequence to a Value

def reduce(f, s, initial): """Combine elements of s pairwise using f, starting with initial. E.g., reduce(mul, [2, 4, 8], 1) is equivalent to mul(mul(mul(1, 2), 4), 8). >>> reduce(mul, [2, 4, 8], 1) 64 """

14

reduce(pow, [1, 2, 3, 4], 2) pow 2 1 2

[ [

slide-78
SLIDE 78

f is ... a two-argument function s is ... a sequence of values that can be the second argument initial is ... a value that can be the first argument

Reducing a Sequence to a Value

def reduce(f, s, initial): """Combine elements of s pairwise using f, starting with initial. E.g., reduce(mul, [2, 4, 8], 1) is equivalent to mul(mul(mul(1, 2), 4), 8). >>> reduce(mul, [2, 4, 8], 1) 64 """

14

reduce(pow, [1, 2, 3, 4], 2) pow 2 1 pow 2

[ [

slide-79
SLIDE 79

f is ... a two-argument function s is ... a sequence of values that can be the second argument initial is ... a value that can be the first argument

Reducing a Sequence to a Value

def reduce(f, s, initial): """Combine elements of s pairwise using f, starting with initial. E.g., reduce(mul, [2, 4, 8], 1) is equivalent to mul(mul(mul(1, 2), 4), 8). >>> reduce(mul, [2, 4, 8], 1) 64 """

14

reduce(pow, [1, 2, 3, 4], 2) pow 2 1 pow 2 2

[ [

slide-80
SLIDE 80

f is ... a two-argument function s is ... a sequence of values that can be the second argument initial is ... a value that can be the first argument

Reducing a Sequence to a Value

def reduce(f, s, initial): """Combine elements of s pairwise using f, starting with initial. E.g., reduce(mul, [2, 4, 8], 1) is equivalent to mul(mul(mul(1, 2), 4), 8). >>> reduce(mul, [2, 4, 8], 1) 64 """

14

reduce(pow, [1, 2, 3, 4], 2) pow 2 1 pow 2 2 4

[ [

slide-81
SLIDE 81

f is ... a two-argument function s is ... a sequence of values that can be the second argument initial is ... a value that can be the first argument

Reducing a Sequence to a Value

def reduce(f, s, initial): """Combine elements of s pairwise using f, starting with initial. E.g., reduce(mul, [2, 4, 8], 1) is equivalent to mul(mul(mul(1, 2), 4), 8). >>> reduce(mul, [2, 4, 8], 1) 64 """

14

reduce(pow, [1, 2, 3, 4], 2) pow 2 1 pow pow 2 2 4

[ [

slide-82
SLIDE 82

f is ... a two-argument function s is ... a sequence of values that can be the second argument initial is ... a value that can be the first argument

Reducing a Sequence to a Value

def reduce(f, s, initial): """Combine elements of s pairwise using f, starting with initial. E.g., reduce(mul, [2, 4, 8], 1) is equivalent to mul(mul(mul(1, 2), 4), 8). >>> reduce(mul, [2, 4, 8], 1) 64 """

14

reduce(pow, [1, 2, 3, 4], 2) pow 2 1 pow pow 2 2 4 3

[ [

slide-83
SLIDE 83

f is ... a two-argument function s is ... a sequence of values that can be the second argument initial is ... a value that can be the first argument

Reducing a Sequence to a Value

def reduce(f, s, initial): """Combine elements of s pairwise using f, starting with initial. E.g., reduce(mul, [2, 4, 8], 1) is equivalent to mul(mul(mul(1, 2), 4), 8). >>> reduce(mul, [2, 4, 8], 1) 64 """

14

reduce(pow, [1, 2, 3, 4], 2) pow 2 1 pow pow 2 2 4 3 64

[ [

slide-84
SLIDE 84

f is ... a two-argument function s is ... a sequence of values that can be the second argument initial is ... a value that can be the first argument

Reducing a Sequence to a Value

def reduce(f, s, initial): """Combine elements of s pairwise using f, starting with initial. E.g., reduce(mul, [2, 4, 8], 1) is equivalent to mul(mul(mul(1, 2), 4), 8). >>> reduce(mul, [2, 4, 8], 1) 64 """

14

reduce(pow, [1, 2, 3, 4], 2) pow 2 1 pow pow pow 2 2 4 3 64

[ [

slide-85
SLIDE 85

f is ... a two-argument function s is ... a sequence of values that can be the second argument initial is ... a value that can be the first argument

Reducing a Sequence to a Value

def reduce(f, s, initial): """Combine elements of s pairwise using f, starting with initial. E.g., reduce(mul, [2, 4, 8], 1) is equivalent to mul(mul(mul(1, 2), 4), 8). >>> reduce(mul, [2, 4, 8], 1) 64 """

14

reduce(pow, [1, 2, 3, 4], 2) pow 2 1 pow pow pow 2 2 4 3 64 4

[ [

slide-86
SLIDE 86

f is ... a two-argument function s is ... a sequence of values that can be the second argument initial is ... a value that can be the first argument

Reducing a Sequence to a Value

def reduce(f, s, initial): """Combine elements of s pairwise using f, starting with initial. E.g., reduce(mul, [2, 4, 8], 1) is equivalent to mul(mul(mul(1, 2), 4), 8). >>> reduce(mul, [2, 4, 8], 1) 64 """

14

reduce(pow, [1, 2, 3, 4], 2) pow 2 1 pow pow pow 2 2 4 3 64 4 16,777,216

[ [

slide-87
SLIDE 87

f is ... a two-argument function s is ... a sequence of values that can be the second argument initial is ... a value that can be the first argument

Reducing a Sequence to a Value

def reduce(f, s, initial): """Combine elements of s pairwise using f, starting with initial. E.g., reduce(mul, [2, 4, 8], 1) is equivalent to mul(mul(mul(1, 2), 4), 8). >>> reduce(mul, [2, 4, 8], 1) 64 """

14

reduce(pow, [1, 2, 3, 4], 2) pow 2 1 pow pow pow 2 2 4 3 64 4 16,777,216

[ [

(Demo)