我是愚蠢的还是在Python 2.5中“断言”? [英] Am I stupid or is 'assert' broken in Python 2.5??

查看:65
本文介绍了我是愚蠢的还是在Python 2.5中“断言”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到Python 2.5中有些奇怪的东西,即2个参数

版本的''assert''被打破了。或者至少对我来说似乎是这样。


在Python解释器中运行以下代码:


myString =无


断言(myString,''字符串为空或设置为None类型!''



断言(myString)


你会注意到第一个断言没有做任何事情,而

秒断言正确识别出myString没有评估为

true。这看起来不对。当然Python应该在第一个断言语句中引发了一个

断言错误,对吧?

I''ve noticed something odd in Python 2.5, namely that the 2 argument
version of ''assert'' is broken. Or at least it seems that way to me.

Run the following code in your Python interpreter:

myString = None

assert( myString, ''The string is either empty or set to the None type!''
)
assert( myString )

You''ll notice that the first assert doesn''t do anything, whereas the
second assert correctly recognizes that myString does not evaluate to
true. That doesn''t seem right. Surely Python should have raised an
assertion error on the first assert statement, right??

推荐答案

没关系,我是个笨蛋! = 0


应该是

断言myString,''字符串为空或无!''

抱歉,不要理我。 = \

Never mind, I''m a schmuck!! =0

It should have been
assert myString, ''String empty or None!''
Sorry, ignore me. =\


antredaécrit:
antred a écrit :

我注意到一些奇怪的事情Python 2.5,即2个参数

版本的''assert''被打破了。或者至少对我来说似乎是这样。


在Python解释器中运行以下代码:


myString =无


断言(myString,''字符串为空或设置为无

类型!'')断言(myString)


你会注意到第一个断言没有做任何事情,而

秒断言正确识别出myString没有评估为

为真。这看起来不对。当然Python应该在第一个断言语句中引发了一个

断言错误,对吧?
I''ve noticed something odd in Python 2.5, namely that the 2 argument
version of ''assert'' is broken. Or at least it seems that way to me.

Run the following code in your Python interpreter:

myString = None

assert( myString, ''The string is either empty or set to the None
type!'' ) assert( myString )

You''ll notice that the first assert doesn''t do anything, whereas the
second assert correctly recognizes that myString does not evaluate to
true. That doesn''t seem right. Surely Python should have raised an
assertion error on the first assert statement, right??



这种行为已经存在于Python中很长一段时间了。只知道

断言不是一个函数,因此它不需要括号(

就像打印一样不需要它们)尝试没有它们并且它会工作

That behaviour has been present in Python for a long time. Just know
that assert is NOT a function, and thus it doesn''t require parenthesis (
just the same as print doesn''t require them ) Try without them and it''ll
work.


In< 11 **************** ******@73g2000cwn.googlegroups。 com>,antred写道:
In <11**********************@73g2000cwn.googlegroups. com>, antred wrote:

在Python解释器中运行以下代码:


myString =无


断言(myString,''字符串为空或设置为None类型!''



断言(myString)


你会注意到第一个断言没有做任何事情,而

秒断言正确识别出myString没有评估为

true。这看起来不对。当然Python应该在第一个断言语句中引发了一个

断言错误,对吧?
Run the following code in your Python interpreter:

myString = None

assert( myString, ''The string is either empty or set to the None type!''
)
assert( myString )

You''ll notice that the first assert doesn''t do anything, whereas the
second assert correctly recognizes that myString does not evaluate to
true. That doesn''t seem right. Surely Python should have raised an
assertion error on the first assert statement, right??



``assert``是一个语句,而不是一个函数。并且非空元组是真实的:


断言(假,''boink'')


这相当于` `断言真``。


Ciao,

Marc''BlackJack''Rintsch

``assert`` is a statement, not a function. And non-empty tuples are "true":

assert (False, ''boink'')

This is equivalent to ``assert True``.

Ciao,
Marc ''BlackJack'' Rintsch


这篇关于我是愚蠢的还是在Python 2.5中“断言”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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