`
simohayha
  • 浏览: 1388972 次
  • 性别: Icon_minigender_1
  • 来自: 火星
社区版块
存档分类
最新评论

python中的Error-checking策略

阅读更多
  参考 python in nutshell 的第三版.

   1.在python中异常被认为无论什么时候只要能使程序更简单更强大就可以被使用。甚至异常可以被频繁的抛出.

   2.在很多语言中,他们所遵从的是"look before you leap" (LBYL),也就是说,在尝试一个操作之前首先应该先检查一下.这里我的理解就是LBYL只是关心语法而不是关心语义的表达。可是python中遵从的是"it's easier to ask forgiveness than permission"既(EAFP),这也是cobol所遵从的,呵呵我在这里的理解是什么都不管先做再说。EAFP更注重语义的表达。
   
  可以通过代码来比较LBYL与EAFP的不同,按照LBYL中我们的代码可能是这样

def safe_divide_1(x, y):
    if y==0:
        print "Divide-by-0 attempt detected"
        return None
    else:
        return x/y

  
  而按照EAFP我们的代码就是这样:
def safe_divide_2(x, y):
    try:
        return x/y
    except ZeroDivisionError:
        print "Divide-by-0 attempt detected"
        return None

  
 3而在使用EAFP风格的时候要注意使用else,比如下面的代码:
  
def trycalling(obj, attrib, default, *args, **kwds):
    try: return getattr(obj, attrib)(*args, **kwds)
    except AttributeError: return default

在这个代码中当如果当getattr的传进多个参数时程序就会出错,因此代码应当改为下面的
def trycalling(obj, attrib, default, *args, **kwds):
    try: method = getattr(obj, attrib)
    except AttributeError: return default
    else:return method(*args,**kwds)

 4 在python in nutshell 中,作者提出了LBYL的几点不足:
  
引用

The checks may diminish the readability and clarity of the common, mainstream cases where everything is okay.

The work needed for checking may duplicate a substantial part of the work done in the operation itself.

The programmer might easily err by omitting some needed check.

The situation might change between the moment the checks are performed and the moment the operation is attempted.


感觉作者很是推崇EAFP,不知道各位怎么看LBYL和EAFP?

分享到:
评论
2 楼 simohayha 2007-03-06  
刑天战士 写道
不仅仅是python吧,在java,C#中,也推崇这种形式。但不是任何时候都要用,比如:
   while(reader.read()!=EOF)

的时候就不应该用异常


呵呵,也没说就python用呀,不过python中的异常和java中的异常最大的区别。我感觉就是java中的异常只是为了语法来服务而不是为了语义,只能使用于一部分的情况,就像effictive java中所说的仅在异常情况下使用异常,而在python中的话一切都是为了程序的简洁,强大来考虑的,只要能满足这些情况,异常可以随便抛(就像一开始所说的).
1 楼 刑天战士 2007-03-06  
不仅仅是python吧,在java,C#中,也推崇这种形式。但不是任何时候都要用,比如:
   while(reader.read()!=EOF)

的时候就不应该用异常

相关推荐

    Scripts for visdom

    使用pytorch为了做实时可视化,在安装visdom之后 (pip install visdom), 第一次使用python -m visdom.server时,会要求下载一些Scripts。 由于网络原因,有时候很难成功下载。这里提供已经下载好了的文件,直接将...

    修复 there was an error checking the latest version of pip

    warning: there was an error checking the latest version of pip.

    Python Cookbook英文版

    6.6 Capturing the Output and Error Streams from a Unix Shell Command 6.7 Forking a Daemon Process on Unix 6.8 Determining if Another Instance of a Script Is Already Running in Windows 6.9 ...

    Python Cookbook, 2nd Edition

    Python Cookbook, 2nd Edition By David Ascher, Alex Martelli, Anna Ravenscroft Publisher : O'Reilly Pub Date : March 2005 ISBN : 0-596-00797-3 Pages : 844 Copyright Preface The ...

    Beginning Python (2005).pdf

    Checking It Against the DTD 301 Another Real-World Problem 301 Try It Out: Creating An Aggregator 301 Summary 303 Exercises 303 Chapter 16: Network Programming 305 Try It Out: Sending Some E-...

    python3.6.5参考手册 chm

    Python参考手册,官方正式版参考手册,chm版。以下摘取部分内容:Navigation index modules | next | Python » 3.6.5 Documentation » Python Documentation contents What’s New in Python What’s New In ...

    matlab导入excel代码-utl_sas_defensive_programming_and_error_checking:SAS防御性

    matlab导入excel代码utl_sas_defensive_programming_and_error_checking SAS防御性编程和错误检查关键字:sas sql连接合并合并大数据分析宏oracle teradata mysql sas社区stackoverflow统计信息人工智慧AI Python R ...

    node-sass.zip

    gyp verb check python checking for Python executable "python2" in the PATH gyp verb `which` failed Error: not found: python2 gyp verb `which` failed at getNotFoundError ``` 这个问题有两个解决方案 ...

    基于mediastreamer2的网络电话实现流程以及源码库

    error: /user/include/python2.7/pyconfig.h:15:52: fatal error: arm-linux-gnueabi/python2.7/pyconfig.h: No such file or directory compilation terminated. 分析::找不到arm-linux-gnueabi/python2.7/...

    SWE-HW:软件工程作业

    要运行错误检查版本:python3 jump_year_error_checking.py 只接受正整数。 如果使用linux和Python3进行了测试,则无论出于何种原因它都不能在Windows上运行,则可能必须删除shebang,它是文件的第一行。

    BIND-LINUX64位包

    checking ndots when searching. Only continue searching on NXDOMAIN responses. [RT #34711] Handle changes to sig-validity-interval settings better. [RT #34625] Fix bug where journal filename ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    Table of Contents Header Files The #define Guard Header File Dependencies Inline Functions The -inl.h Files Function Parameter Ordering Names and Order of Includes Scoping Namespaces Nested Classes ...

    LuaBind 源码 (Lua增强库)

    * 返回值策略和参数策略 3 可移植性 LuaBind 已经通过下面的编译器环境的测试: Visual Studio 7.1 Visual Studio 7.0 Visual Studio 6.0 (sp 5) Intel C++ 6.0 (Windows) GCC 2.95.3 (cygwin) GCC 3.0.4 (Debian/...

    pro_apache_third_edition..pdf

    Contents About the Author...............................................................................................xix About the Technical Reviewer and Contributing Author.................xxi ...

Global site tag (gtag.js) - Google Analytics