Python比Java更不容易出错 [英] Python less error-prone than Java

查看:45
本文介绍了Python比Java更不容易出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你会经常听到由于故障最小化的原因,你应该使用严格打字的编程语言:
http://turing.une.edu.au/~comp284/Le...ure/node1。 html


我刚刚遇到一个有趣的例子,情况恰恰相反。


以下是二分查找Java中的算法。它在一个有序数组中搜索一个值




public static int binarySearch(int [] a,int key){

int low = 0;

int high = a.length - 1;

while(low< = high){

int mid =(low + high)/ 2;

int midVal = a [mid];

if(midVal< key)

low = mid + 1;

else if(midVal> key)

high = mid - 1;

else

返回中期; //找到钥匙

}

返回 - (低+ 1); //找不到密钥。

}


现在同样的事情,直接转换为Python:


def binarySearch(a,key):

low = 0

high = len(a) - 1

而low< = high:

mid =(低+高)/ 2

midVal = a [mid]

如果midVal< key:

low = mid + 1

elif midVal> key:

high = mid - 1;

else:

返回mid#key found

return - (低+ 1)找不到#键。


Python版本有什么好处?首先,它将在* any *

排序数组上运行,无论值是哪种类型。


但其次,存在隐藏的错误$

版本没有的Java版本。


如果找不到,请参阅以下网页;-)
http://googleresearch.blogspot .com / 2 ... it-almost.html


- Christoph

解决方案

< blockquote>实际上,你在各个层面都错了。


第一:在Java中创建一个排序的二进制排序非常简单

所有包含对象的数组;在那里错了。


其次:这个bug与静态打字无关(我猜是这么说你的意思就是
。两者都是Python和Java是强类型的)。

问题是整数都是用Java限制的。他们可以很容易地获得
整数然后自动强制到(相当于)多头,当他们b
变大时;他们不是一个设计错误,而不是任何事情要做静态打字的
。如果在int太大的情况下引发了一个

溢出异常,那么Python中的等价物就是这样。它可能有这样的方式,键入或不打字。


Christoph Zwerschke写道:

你会经常听到故障最小化的原因,你应该使用严格打字的编程语言:
http://turing.une.edu.au/~comp284/Le...ure/node1.html

我刚刚遇到了一个有趣的例子,其中情况恰恰相反。

以下是Java中的二进制搜索算法。它在一个有序数组a中搜索一个值:

public static int binarySearch(int [] a,int key){
int low = 0;
int high = a.length - 1;
while(low <= high){
int mid =(low + high)/ 2;
int midVal = a [mid];
if(midVal< key)
low = mid + 1;
else if(midVal> key)
high = mid - 1;
else
返回中期; //找到钥匙
}
返回 - (低+ 1); //找不到密钥。

现在同样的事情,直接转换为Python:

def binarySearch(a,key):
低= 0
高= len(a) - 1
虽然低< =高:
mid =(低+高)/ 2
midVal = a [mid]
如果midVal< key:
low = mid + 1
elif midVal>关键:
高=中 - 1;
否则:
返回中间#键找到
返回 - (低+ 1)#键未找到。
Python版本有什么好处?首先,它将在* any *
排序的数组上运行,无论值是哪种类型。

但其次,Java版本中存在隐藏的错误。 >版本没有。

如果你没找到它,请看下面的网页;-)
http://googleresearch.blogspot.com/2...it-nearly.html

- Christoph




Simon Percivall< pe ******* @ gmail.com> ;写道:

...

静态类型。如果在int变得太大时引发了
溢出异常,那么Python中的等价物就会出现。它可能就是这样,打字或者没有打字。




的确,它_used_就是这样 -

< HTTP://docs.python.org/lib/module-exceptions.html> STILL说...:


异常溢出错误


当算术运算的结果太大而无法使用时引发

代表。这对于长整数来说是不可能发生的(相反,b
会引发MemoryError而不是放弃)。由于C中缺少标准化

的浮点异常处理,大多数浮点运算
操作也不会被检查。对于普通整数,除了左移之外,检查所有

可以溢出的操作,其中典型的应用程序

更喜欢丢弃位而不是引发异常。

实际上,文档在这一点上已经过时了,当有必要时,int会变成很长的


sys.maxint + 1



2147483648L


但是,这个操作_would_已经在旧的足够多的

版本的Python中引发了OverflowError(不确定转换发生的时间......)。

Alex


文章< e5 ********** @ online.de>,

Christoph Zwerschke< ci ** @ online.de>写道:

你会经常听到由于故障最小化的原因,你应该使用严格打字的编程语言:
http://turing.une.edu.au/~comp284/Le...ure /node1.html

我刚刚遇到一个有趣的例子,情况正好相反。



You will often hear that for reasons of fault minimization, you should
use a programming language with strict typing:
http://turing.une.edu.au/~comp284/Le...ure/node1.html

I just came across a funny example in which the opposite is the case.

The following is a binary search algorithm in Java. It searches a value
in an ordered array a of ints:

public static int binarySearch(int[] a, int key) {
int low = 0;
int high = a.length - 1;
while (low <= high) {
int mid = (low + high) / 2;
int midVal = a[mid];
if (midVal < key)
low = mid + 1;
else if (midVal > key)
high = mid - 1;
else
return mid; // key found
}
return -(low + 1); // key not found.
}

Now the same thing, directly converted to Python:

def binarySearch(a, key):
low = 0
high = len(a) - 1
while low <= high:
mid = (low + high) / 2
midVal = a[mid]
if midVal < key:
low = mid + 1
elif midVal > key:
high = mid - 1;
else:
return mid # key found
return -(low + 1) # key not found.

What''s better about the Python version? First, it will operate on *any*
sorted array, no matter which type the values have.

But second, there is a hidden error in the Java version that the Python
version does not have.

See the following web page if you dont find it ;-)
http://googleresearch.blogspot.com/2...it-nearly.html

-- Christoph

解决方案

Actually, you''re wrong on all levels.

First: It''s perfectly simple in Java to create a binary sort that sorts
all arrays that contain objects; so wrong there.

Secondly: The bug has nothing to do with static typing (I''m guessing
that''s what you meant. Both Python and Java are strongly typed). The
problem is that ints are bounded in Java. They could easily have been
ints and then automatically coerced to (equivalent to) longs when they
got bigger; that they aren''t is more a design fault than anything to do
with static typing. The equivalent in Python would have been if an
overflow exception was raised when the int got too big. It might have
been that way, typing or no typing.

Christoph Zwerschke wrote:

You will often hear that for reasons of fault minimization, you should
use a programming language with strict typing:
http://turing.une.edu.au/~comp284/Le...ure/node1.html

I just came across a funny example in which the opposite is the case.

The following is a binary search algorithm in Java. It searches a value
in an ordered array a of ints:

public static int binarySearch(int[] a, int key) {
int low = 0;
int high = a.length - 1;
while (low <= high) {
int mid = (low + high) / 2;
int midVal = a[mid];
if (midVal < key)
low = mid + 1;
else if (midVal > key)
high = mid - 1;
else
return mid; // key found
}
return -(low + 1); // key not found.
}

Now the same thing, directly converted to Python:

def binarySearch(a, key):
low = 0
high = len(a) - 1
while low <= high:
mid = (low + high) / 2
midVal = a[mid]
if midVal < key:
low = mid + 1
elif midVal > key:
high = mid - 1;
else:
return mid # key found
return -(low + 1) # key not found.

What''s better about the Python version? First, it will operate on *any*
sorted array, no matter which type the values have.

But second, there is a hidden error in the Java version that the Python
version does not have.

See the following web page if you dont find it ;-)
http://googleresearch.blogspot.com/2...it-nearly.html

-- Christoph




Simon Percivall <pe*******@gmail.com> wrote:
...

with static typing. The equivalent in Python would have been if an
overflow exception was raised when the int got too big. It might have
been that way, typing or no typing.



Indeed, it _used_ to be that way --
<http://docs.python.org/lib/module-exceptions.html> STILL says...:

exception OverflowError

Raised when the result of an arithmetic operation is too large to be
represented. This cannot occur for long integers (which would rather
raise MemoryError than give up). Because of the lack of standardization
of floating point exception handling in C, most floating point
operations also aren''t checked. For plain integers, all operations that
can overflow are checked except left shift, where typical applications
prefer to drop bits than raise an exception.
Actually, the docs are obsolete on this point, and an int becomes a long
when that''s necessary:

sys.maxint+1


2147483648L

but, this operation _would_ have raised OverflowError in old-enough
versions of Python (not sure exactly when the switch happened...).
Alex


In article <e5**********@online.de>,
Christoph Zwerschke <ci**@online.de> wrote:

You will often hear that for reasons of fault minimization, you should
use a programming language with strict typing:
http://turing.une.edu.au/~comp284/Le...ure/node1.html

I just came across a funny example in which the opposite is the case.



这篇关于Python比Java更不容易出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆