python debug 方法

最後更新: 2015-04-09

目錄

  • print
  • pprint
  • var_dump
  • Debug Mode

 


print

 

print repr(context)

<lxml.etree.iterparse object at 0x9f22094>

print dir(context)

['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__iter__', '__new__', '__next__', '__pyx_vtable__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'copy', 'error_log', 'makeelement', 'next', 'resolvers', 'root', 'setElementClassLookup', 'set_element_class_lookup', 'target', 'version']

 


pprint — Data pretty printer

 

Install

pip install pprint

Usage

class pprint.PrettyPrinter(indent=1, width=80, depth=None, stream=None)

Example

from pprint import pprint

pprint(var)

 


var_dump

 

Install:

pip install var_dump

Usage:

#!/usr/bin/env python

from var_dump import var_dump

testvar={'test1':1, 'test2':'string', 'test3':3.14}

var_dump(testvar)

Output:

#0 dict(3)
    ['test1'] => int(1)
    ['test3'] => float(3.14)
    ['test2'] => str(6) "string"

 

 


Debug Mode

 

DEBUG=True

if DEBUG: print "on debug mode"

 

Creative Commons license icon Creative Commons license icon