作为控制结构的例外 [英] Exceptions as a Control Structure

查看:65
本文介绍了作为控制结构的例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我是Python的新手(我刚刚完成了Guido的教程)。

我很惊讶地发现那里StopIteration

用于在标准迭代器设置中结束循环。


我来自C ++,其中使用异常作为控件
出于效率原因,
结构是不受欢迎的。


这个主题的Python经典是什么?是例外

被认为是合理的控制结构,还是

单独使用StopIteration?


问候,

Olivier。

解决方案

Olivier Parisy写道:

我是Python新手(我刚刚完成了Guido的教程。
我很惊讶地发现StopIteration
用于在标准迭代器设置中结束循环。

我来自C ++,由于效率原因,使用异常作为控制结构是不受欢迎的。


在Python中,很少有人因为效率而感到不满

的原因。有一些例子,包括反复使用+ =

来增长字符串,而某些字段效率

当然是关键的,但在Python中几乎总是看起来很好/>
用于最实用的方法(对于某些人的优雅定义,这通常也是最优雅的b $ b优雅)

而不是痴迷关于速度。

这个主题的Python经典是什么?


唯一的Pythoncanon可以通过在解释器提示符下键入import this

找到。 ;-)

是例外
被视为合理的控制结构,还是单独使用StopIteration?




捕获异常被认为是一种合理的方法

来控制各种问题。例如,

是在深层嵌套的循环中,而不是走出

,以避免异常支持旗帜和批次,一个只是提出一个自定义异常

(或者可能是一个标准的异常)并在

循环之外捕获它。


异常也被一些人用作从子程序返回信息的机制,但是它很可能信息仍然是b $ b被视为

例外在某种程度上(把它想象成一个带外的

机制来返回特殊信息)。


虽然有些人反对文体(或者甚至

性能问题,在某些情况下)对于这样的事情,你可能会发现Python程序员更容易发现异常被抛出了b / b
比你习惯的那样。


另请注意,在许多情况下除了算法之外,还有b $ b复杂性,你在
$ b中学到了很多关于效率的知识$ b C ++应该被认为是可疑的信息来源于Python的
。例如,函数调用在Python中比在C ++中要贵得多,因为设置调用帧需要时间
。另外一方面,另外一方面,例外情况更为有效。

一些谷歌集团在clp中的搜索可能会导致很多b $ b导致很多过去对这些事情的讨论。


-Peter


文章< 41 ********** *************@news.free.fr> ;,

Olivier Parisy< ol ************ @ free.fr> ;写道:

大家好,

我是Python新手(我刚刚完成了Guido的教程)。
我很惊讶在那里学习StopIteration
用于在标准迭代器设置中结束循环。

我来自C ++,其中使用异常作为控件
结构是不受欢迎的效率原因。
关于这个主题的Python经典是什么?是例外
被认为是合理的控制结构,还是单独使用StopIteration?




你在C ++中做的很多事情都不赞成在Python中。很多东西你用Python做的
在C ++中是不受欢迎的。


具体来说,C ++中的异常通常被认为是相当的

重量级,但在Python中则不然。这是另一个常见的python成语

,它使用异常的方式可能会让大多数C ++感到恐惧

人:


按键顺序:

试试:

foo =字典[key]

除了KeyError

foo =" ;找不到


而不是:


关键顺序:

如果是dictionary.has_key(关键字) ):

foo =字典[key]

else

foo =" not found"


如果您有理由确定大多数键都可以在

字典中找到,那么只需尝试一下就可以更快地处理

偶尔的异常,而不是测试每个键是否存在。


Peter Hansen写道:

在Python中,很少仅仅因为效率原因而不满意。有一些例子,包括反复使用+ =
来增长字符串,以及效率当然至关重要的一些字段,但在Python中,几乎总是看起来最实用的方法(对于一些人来说,优雅的定义往往也是最优雅的,而不是对速度的迷恋。


我理解这一点。这是我学习的原因之一

Python(我更感兴趣的是改善我的开发时间

比我的程序的执行时间更长)。

是异常
被视为合理的控制结构,还是单独使用StopIteration?



捕获异常被认为是一种合理的方法来控制各种问题。例如,一个是深度嵌套的循环,而不是走出一个方法,以避免异常,有利于标志和许多尴尬的测试,一个只是提出一个自定义异常
(或者可能是标准异常)并将其捕获到
循环之外。




好​​的。绝对不是我在C ++中做的事情。

例外也被一些人用作从子程序返回信息的机制,尽管它很可能是信息仍将被视为特殊信息在某种程度上(把它看作是一种返回特殊信息的带外
机制)。


你在标准库中有这方面的一些例子吗?

虽然有些人反对文体理由(甚至是表现性的,但是在某些情况下,你可能会发现Python程序员比你习惯的更容易发现异常。


这就是我想知道的。 OK。

另请注意,在许多情况下,除了算法复杂性之外,您在C ++中学到的有关效率的知识应该被视为可疑信息。蟒蛇。例如,由于设置调用帧所需的时间,函数调用在Python中比在C ++中更昂贵。另一方面,例外情况,我记得更有效率。


所以我想我只是不关心自己的效率

问题并专注于prope风格,然后。

一些谷歌集团在clp中的搜索可能会导致许多过去对这些事情的讨论。




我必须承认我可能没做我的功课。 ..

BTW,感谢您的反馈!


问候,

奥利维尔。


Hi all,

I am new to Python (I just finished Guido''s tutorial).
I was very surprised to learn there that the StopIteration
is used to end for loops in a standard iterator setting.

I come from C++, where the use of exceptions as control
structures is frowned upon for efficiency reasons.

What is the Python canon on this topic ? Are exceptions
considered as reasonable control structures, or is
StopIteration alone of its kind ?

Regards,
Olivier.

解决方案

Olivier Parisy wrote:

I am new to Python (I just finished Guido''s tutorial).
I was very surprised to learn there that the StopIteration
is used to end for loops in a standard iterator setting.

I come from C++, where the use of exceptions as control
structures is frowned upon for efficiency reasons.
In Python, very little is frowned upon solely for efficiency
reasons. There are some examples, including using +=
repeatedly to grow a string, and some fields where efficiency
is of course critical, but in Python one almost always looks
for the most pragmatic approach (which is often the most
elegant, too, for some people''s definition of elegant)
rather than obsessing about speed.
What is the Python canon on this topic ?
The only Python "canon" can be found by typing "import this"
at the interpreter prompt. ;-)
Are exceptions
considered as reasonable control structures, or is
StopIteration alone of its kind ?



Catching an exception is considered a reasonable approach
to flow control for various problems. One, for example,
is in deeply nested loops, where rather than going out of
one''s way to avoid exceptions in favour of a flag and lots
of awkward testing, one just raises a custom exception
(or perhaps a standard one) and catches it outside the
loops.

Exceptions are also used by some as a mechanism for
returning information from subroutines, though it''s
very likely the information will still be considered
"exceptional" in some way (think of it as an out-of-band
mechanism for returning special info).

While some people object on stylistic grounds (or even
performance ones, in some cases) to such things, you
will likely find that exceptions are thrown around
by Python programmers more readily than you are used to.

Note also that in many cases other than algorithmic
complexity, what you have learned about efficiency in
C++ should be considered suspect info when it comes
to Python. Function calls, for example, are much more
expensive in Python than in C++ because of the time
required to set up the call frame. Exceptions, on the
other hand, are as I recall much more efficient.
Some Google Group searches in c.l.p would probably
lead to many past discussions of these things.

-Peter


In article <41***********************@news.free.fr>,
Olivier Parisy <ol************@free.fr> wrote:

Hi all,

I am new to Python (I just finished Guido''s tutorial).
I was very surprised to learn there that the StopIteration
is used to end for loops in a standard iterator setting.

I come from C++, where the use of exceptions as control
structures is frowned upon for efficiency reasons.

What is the Python canon on this topic ? Are exceptions
considered as reasonable control structures, or is
StopIteration alone of its kind ?



Lots of things you do in C++ are frowned upon in Python. Lots of things
you do in Python are frowned upon in C++.

Specifically, exceptions in C++ are generally considered fairly
heavy-weight, but not so in Python. Here''s another common python idiom
that uses exceptions in a way which would probably horrify most C++
people:

for key in sequence:
try:
foo = dictionary [key]
except KeyError
foo = "not found"

instead of:

for key in sequence:
if dictionary.has_key (key):
foo = dictionary [key]
else
foo = "not found"

If you''re reasonably sure that most of the keys will be found in the
dictionary, it''s probably faster to just try them all and handle the
occasional exception than to test each key to see if it exists.


Peter Hansen wrote:

In Python, very little is frowned upon solely for efficiency
reasons. There are some examples, including using +=
repeatedly to grow a string, and some fields where efficiency
is of course critical, but in Python one almost always looks
for the most pragmatic approach (which is often the most
elegant, too, for some people''s definition of elegant)
rather than obsessing about speed.
I understand this. That''s one of the reasons I am learning
Python (I am more interested in improving my development time
than my programs'' execution time).

Are exceptions
considered as reasonable control structures, or is
StopIteration alone of its kind ?


Catching an exception is considered a reasonable approach
to flow control for various problems. One, for example,
is in deeply nested loops, where rather than going out of
one''s way to avoid exceptions in favour of a flag and lots
of awkward testing, one just raises a custom exception
(or perhaps a standard one) and catches it outside the
loops.



OK. Definitely not something I''d do in C++.
Exceptions are also used by some as a mechanism for
returning information from subroutines, though it''s
very likely the information will still be considered
"exceptional" in some way (think of it as an out-of-band
mechanism for returning special info).
Do you have some example of this in the standard library ?
While some people object on stylistic grounds (or even
performance ones, in some cases) to such things, you
will likely find that exceptions are thrown around
by Python programmers more readily than you are used to.
That''s what I wanted to know. OK.
Note also that in many cases other than algorithmic
complexity, what you have learned about efficiency in
C++ should be considered suspect info when it comes
to Python. Function calls, for example, are much more
expensive in Python than in C++ because of the time
required to set up the call frame. Exceptions, on the
other hand, are as I recall much more efficient.
So I suppose I just won''t concern myself with efficiency
questions and concentrate on prope style, then.
Some Google Group searches in c.l.p would probably
lead to many past discussions of these things.



I must admit I probably didn''t do my homework...
BTW, thanks for your reactivity !

Regards,
Olivier.


这篇关于作为控制结构的例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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