#糟糕的代码风格 try: do_something() except: pass #值得提倡的代码风格 try: do_something() except ValueError: # Catch some very specific exception - KeyError, ValueError, etc. pass
#会引发Exception的函数样例 def get_number(): return int('foo')
#利用logging模块 [最佳实践__使用默认Python库已经提供的方法而不是自己去写] import logging try: x = get_number() except Exception as ex: logging.exception('Caught an error')
# The Python 2.x version. It's a little less work. import sys import traceback def log_traceback(ex, ex_traceback): tb_lines = traceback.format_exception(ex.__class__, ex, ex_traceback) tb_text = ''.join(tb_lines) # I'll let you implement the ExceptionLogger class, and the timestamping. exception_logger.log(tb_text) try: x = get_number() except Exception as ex: # Here, I don't really care about the first two values. # I just want the traceback. _, _, ex_traceback = sys.exc_info() log_traceback(ex, ex_traceback)
# The Python 3 version. It's a little less work. import traceback def log_traceback(ex): tb_lines = traceback.format_exception(ex.__class__, ex, ex.__traceback__) tb_text = ''.join(tb_lines) # I'll let you implement the ExceptionLogger class, and the timestamping. exception_logger.log(tb_text) try: x = get_number() except Exception as ex: log_traceback(ex)
import sys import traceback def handle_exeception(e): traceback.print_exc() try: raise Exception except: f = sys.exc_info()[2].tb_frame.f_back print 'EXCEPTION|%s:%s:%d "%s"' %(f.f_code.co_filename, f.f_code.co_name, f.f_lineno, e) return try: x = get_number() except Exception, e: handle_exeception(e)
英文链接:
https://realpython.com/blog/python/the-most-diabolical-python-antipattern/
相关链接:
- https://docs.python.org/2/library/logging.html#logging.exception
- https://docs.python.org/2/library/traceback.html#traceback.print_exc
- https://docs.python.org/2/library/sys.html#sys.exc_info
- =
- http://stackoverflow.com/questions/4508849/how-to-log-python-exception
- http://stackoverflow.com/questions/20241478/how-can-i-get-the-traceback-object-sys-exc-info2-same-as-sys-exc-traceba
- http://stackoverflow.com/questions/8238360/how-to-save-traceback-sys-exc-info-values-in-a-variable
- =
- http://farmdev.com/src/secrets/framehack/index.html
- http://stackoverflow.com/questions/1140194/in-python-how-do-i-obtain-the-current-frame
- http://farmdev.com/src/secrets/framehack/#id4
《 “糟糕的Python编程习惯” 》 有 5 条评论
Python Anti-Patterns – Python 编码的反面案例
https://docs.quantifiedcode.com/python-anti-patterns/latex/The-Little-Book-of-Python-Anti-Patterns-1.0.pdf
Python最差实践
http://blog.guoyb.com/2016/12/03/bad-py-style/
Python 编码风格参考
http://kuanghy.github.io/2017/05/14/python-coding-style
http://kuanghy.github.io/tags/#python
Python 中的 if __name__ == ‘__main__’ 该如何理解
http://kuanghy.github.io/2017/04/24/python-entry-program
Python Tips – 防御性编程风格 EAFP vs LBYL
https://mp.weixin.qq.com/s/r8pUVe67SwaCfZq4PAsjFg
`
“Easier to Ask for Forgiveness than Permission.”(请求宽恕比许可更容易)— EAFP
“Look Before You Leap”(三思而后行 )— LBYL
如果有潜在不可控的问题,使用 EAFP ;
如果预先检查成本很高,请使用 EAFP ;
如果您希望操作在大多数时间成功,请使用 EAFP ;
如果您预计操作失败的时间超过一半,请使用 LBYL ;
如果速度不重要,使用您认为更易读的风格;
`
防御性编程的介绍和技巧
http://blog.jobbole.com/101651/
http://www.cnblogs.com/bakari/archive/2012/08/27/2658215.html
http://www.uml.org.cn/codeNorms/201007165.asp