哪个更'pythonic'/'更好'? [英] which is more 'pythonic' / 'better' ?

查看:72
本文介绍了哪个更'pythonic'/'更好'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




有两个版本的简单代码。

哪个更好?

===

如果len(线)> =(n + 1):

text = line [n]

else:

text =''没有''

===

===

试试:

text = line [n]
除了IndexError之外的


text =''没有''

===

你会使用哪一个?


谢谢,

gabor



there are 2 versions of a simple code.
which is preferred?
===
if len(line) >= (n+1):
text = line[n]
else:
text = ''nothing''
===
===
try:
text = line[n]
except IndexError:
text = ''nothing''
===
which is the one you would use?

thanks,
gabor

推荐答案

2005年9月12日星期一12:52:52 + 0200,gabor写道:
On Mon, 12 Sep 2005 12:52:52 +0200, gabor wrote:


有两个版本的简单代码。
哪个是首选?

===
如果len(line)> =(n + 1):
text = line [n]
否则:
text =''没有''
===

===
尝试:
text = line [n] <除了IndexError:
text =''没有''
===

你将使用哪一个?


there are 2 versions of a simple code.
which is preferred?
===
if len(line) >= (n+1):
text = line[n]
else:
text = ''nothing''
===
===
try:
text = line[n]
except IndexError:
text = ''nothing''
===
which is the one you would use?




就个人而言,我会使用其中之一。你说po-ta-to,我说是pot-at-o。


尝试...除了......块很快就能设置好,但是很快就能找到它/>
例外。如果您希望大多数尝试都能成功,那么

try块通常比每次测试列表长度更快。




但是如果你期望写这条线的尝试经常失败更多

,那么测试会更快。


你需要事先进行自己的测试才能找到确切的

截止值,并期望截止值根据Python

的实现和版本而有所不同。但是一个粗略的经验法则是,如果你希望你的代码失败而不是成功,那么先测试,否则

会遇到异常。


-

史蒂文。



Personally, I would use either. You say po-ta-to, I say pot-at-o.

try...except... blocks are quick to set up, but slow to catch the
exception. If you expect that most of your attempts will succeed, then the
try block will usually be faster than testing the length of the list
each time.

But if you expect that the attempts to write the line will fail more
frequently, then testing will be quicker.

You will need to do your own testing before hand to find the exact
cut-off, and expect that cut-off to vary according to the Python
implementation and version. But a rough rule of thumb is, if you expect
your code to fail more often than succeed, then test first, otherwise
catch an exception.

--
Steven.


gabor写道:
hi ,

有两个版本的简单代码。
这是首选的?

===
如果len(line)> =( n + 1):
text = line [n]
否则:
text =''没有''
===

===
尝试:
text = line [n]
除了IndexError:
text =''没有''
===

哪个是你要使用的那个?


there are 2 versions of a simple code.
which is preferred?
===
if len(line) >= (n+1):
text = line[n]
else:
text = ''nothing''
===
===
try:
text = line[n]
except IndexError:
text = ''nothing''
===
which is the one you would use?




我会在这个特殊情况下使用以下内容..


text = line [n:n + 1]或''没有''


但总的来说我认为最好只使用例如你期望的
的例外情况_not_抛出异常的代码大部分是b
次。否则简单的条件会更好。虽然我期待那里

无论如何都没有太大的区别..

将McGugan

-
http://www.kelpiesoft.com


Will McGugan a crit:
Will McGugan a ??crit :
gabor写道:
gabor wrote:


有两个版本的简单代码。
哪个更受欢迎?

===
如果len(line)> =(n + 1):
text = line [n]
else:
text =''没有''
===

===
尝试:
text = line [n]
除外IndexError:
text =''没问题'
===

哪个是你会用的?

我会实际使用以下对于这个特殊情况..

text = line [n:n + 1]或''nothing''


there are 2 versions of a simple code.
which is preferred?
===
if len(line) >= (n+1):
text = line[n]
else:
text = ''nothing''
===
===
try:
text = line[n]
except IndexError:
text = ''nothing''
===
which is the one you would use?

I would actualy use the following for this particular case..

text = line[n:n+1] or ''nothing''




....你会得到一个元素或一个字符串的列表...

我想你想写:


text =(line [n:n + 1]或[''nothing''])[0]


但是,我不会用它,因为它很难阅读......你必须非常了解Python才能知道:


1 - 表达式是line [i:j],i和j被替换为

" len(line)"如果它们大于len(线)

2 - 所以如果n> len(line),然后line [n:n + 1]" == len [len(line):len(line)]

== []

(行[n:n + 1]行不明显空列表......)

3 - 空列表评估为假

4 - 或 operator返回第一个求值为true的参数


所以,最后,你在同一个小的
表达式中使用了Python的3个副作用...(1,2和4)

但总的来说,我认为最好只使用类似的例外情况
你希望代码在多数情况下都不会抛出异常。
否则简单的条件会更好。虽然我希望两种方式都没有太大区别..

麦克古根会不会
-
http://www.kelpiesoft.com




我要做的是以下内容:

- 如果这发生在一个循环中,我会尽可能删除任何测试并且

在循环外捕获异常!

- 否则,我会去参加测试,因为它更难以阅读。


Pierre



What I would do is the following:
- if this happen in a loop, I would, if possible, remove any test and
catch the exception outside the loop !
- otherwise, I would go for the test, as it is more straitforward to read.

Pierre


这篇关于哪个更'pythonic'/'更好'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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