0

traceback.format_exception() takes three arguments.

sys.exc_info() returns a tuple of three elements that are the required arguments for traceback.format_exception()

Is there any way of avoiding the two line "conversion":

a,b,c = sys.exc_info()
error_info = traceback.format_exception(a,b,c)

Clearly

error_info = traceback.format_exception(sys.exc_info())

doesn't work, because format_exception() takes three arguments, not one tuple (facepalm!)

Is there some tidy way of doing this in one statement?

GreenAsJade
  • 14,459
  • 11
  • 63
  • 98

1 Answers1

2

You can use the * operator to unpack arguments from a list or tuple:

error_info = traceback.format_exception(*sys.exc_info())

Here's the example from the docs:

>>> range(3, 6)             # normal call with separate arguments
[3, 4, 5]
>>> args = [3, 6]
>>> range(*args)            # call with arguments unpacked from a list
[3, 4, 5]
grc
  • 22,885
  • 5
  • 42
  • 63
  • http://stackoverflow.com/a/3394898/334999 check this. A detailed explanation on * and ** – Shuo Oct 10 '14 at 10:35
  • Dang, I was googling for "flatten" where clearly I should have googled for "unpack"! Thanks heaps, accept tick coming in 10 minutes :) – GreenAsJade Oct 10 '14 at 10:36