Skip to content

Instantly share code, notes, and snippets.

@fcicq
Created October 6, 2012 06:47
Show Gist options
  • Save fcicq/3844246 to your computer and use it in GitHub Desktop.
Save fcicq/3844246 to your computer and use it in GitHub Desktop.
Things you didn't know about Python https://speakerdeck.com/u/mitsuhiko/p/didntknow
import sys
import traceback
def dump_threads():
for thread_id, frame in sys._current_frames().iteritems():
print 'Thread #%d' % thread_id
print ''.join(traceback.format_stack(frame))
# Usage: print_frame_info(sys._getframe())
def print_frame_info(frame):
print 'module: %s' % frame.f_globals.get('__name__')
print 'filename: %s' % frame.f_code.co_filename
print 'current line: %d' % frame.f_lineno
loc = dict((k, v) for k, v in frame.f_locals.iteritems() if not k.startswith('__'))
print 'local variables: %s' % loc
@schlamar
Copy link

I would prefer for the latter:

import inspect

def print_frame_info():
    frame = inspect.stack()[1][0]
    ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment