参考 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?
分享到:
- 2007-03-02 21:12
- 浏览 4300
- 评论(2)
- 论坛回复 / 浏览 (2 / 4861)
- 查看更多
相关推荐
使用pytorch为了做实时可视化,在安装visdom之后 (pip install visdom), 第一次使用python -m visdom.server时,会要求下载一些Scripts。 由于网络原因,有时候很难成功下载。这里提供已经下载好了的文件,直接将...
warning: there was an error checking the latest version of pip.
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 By David Ascher, Alex Martelli, Anna Ravenscroft Publisher : O'Reilly Pub Date : March 2005 ISBN : 0-596-00797-3 Pages : 844 Copyright Preface The ...
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-...
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防御性编程和错误检查关键字:sas sql连接合并合并大数据分析宏oracle teradata mysql sas社区stackoverflow统计信息人工智慧AI Python R ...
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 ``` 这个问题有两个解决方案 ...
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/...
要运行错误检查版本:python3 jump_year_error_checking.py 只接受正整数。 如果使用linux和Python3进行了测试,则无论出于何种原因它都不能在Windows上运行,则可能必须删除shebang,它是文件的第一行。
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 ...
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 ...
* 返回值策略和参数策略 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/...
Contents About the Author...............................................................................................xix About the Technical Reviewer and Contributing Author.................xxi ...