Python——捕获异常的效率 [英] Python -- efficiency of caught exceptions

查看:86
本文介绍了Python——捕获异常的效率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
Python 常见问题解答:异常有多快?"

我记得读到 Python 实现了关于异常的寻求宽恕比请求许可更好"的哲学.根据作者的说法,这意味着 Python 代码应该使用大量的 try-except 子句,而不是试图提前确定您是否将要执行会导致异常的操作.

I remember reading that Python implements a "Better to seek forgiveness than to ask permission" philosophy with regards to exceptions. According to the author, this meant Python code should use a lot of try - except clauses, rather than trying to determine ahead of time if you were about to do something that would cause an exception.

我刚刚在我的 Web 应用程序中编写了一些 try - except 子句,其中大部分时间运行代码时都会引发异常.因此,在这种情况下,引发和捕获异常将是常态.从效率的角度来看,这很糟糕吗?我还记得有人告诉我捕获引发的异常会产生很大的性能开销.

I just wrote some try - except clauses on my web app in which an exception will be raised most of the time the code is run. So, in this case, raising and catching an exception will be the norm. Is this bad from an efficiency point of view? I also remember someone telling me that catching a raised exception has a large performance overhead.

使用 try - except 子句是否会不必要地降低效率,您希望在这些子句中几乎所有时间都会引发和捕获异常?

Is it unnecessarily inefficient to use try - except clauses in which you expect an exception to be raised and caught almost all of the time?

这是代码 -- 它使用 Django ORM 检查将用户与各种第三方社交提供商相关联的对象.

Here's the code -- its using the Django ORM to check for objects that associate users with various third party social providers.

try:
    fb_social_auth = UserSocialAuth.objects.get(user=self, provider='facebook')
    user_dict['facebook_id'] = fb_social_auth.uid
except ObjectDoesNotExist:
    user_dict['facebook_id'] = None

try:
    fs_social_auth = UserSocialAuth.objects.get(user=self, provider='foursquare')
    user_dict['foursquare_id'] = fs_social_auth.uid
except ObjectDoesNotExist:
    user_dict['foursquare_id'] = None

try:
    tw_social_auth = UserSocialAuth.objects.get(user=self, provider='twitter')
    user_dict['twitter_id'] = tw_social_auth.uid
except ObjectDoesNotExist:
    user_dict['twitter_id'] = None

第一个很少会例外,因为现在我们正在强制执行使用 Facebook 登录"作为新用户加入网站的主要方法.但是,Twitter 和 Foursquare 是可选的,以防他们想导入朋友或关注者,我预计大多数人不会.

The first one will rarely take the exception, since right now we are enforcing "Sign In With Facebook" as the primary method for new users to join the site. But, Twitter and Foursquare are optional, in case they want to import friends or followers, and I expect most people will not.

我愿意用更好的方法来编写这个逻辑.

I'm open to better ways to code this logic.

推荐答案

每当您编写代码时,都会在关注点之间取得平衡:性能、可读性、正确性、可扩展性、可维护性等.不幸的是,通常不可能同时在每个方向上改进代码.例如,快速的内容可能不那么可读.

Whenever you code there is a balancing of concerns: performance, readability, correctness, extendability, maintainability, etc. Unfortunately, it is often not possible to improve code in each of these directions at the same time. What is fast may not be as readable for instance.

在 Python 中鼓励 try..except 的原因之一是因为您通常无法预测代码的所有使用方式,因此与其检查特定条件是否存在,不如更一般地只捕获可能出现的某一类错误中的任何一个.因此,try..except 可能会使您的代码更可重用.

One of the reasons why try..except is encouraged in Python is because you often can not anticipate all the ways your code may be used, so rather than checking if a specific condition exists, it is more general to just catch any of a certain class of error that might arise. Thus try..except may make your code more reusable.

但是,如果经常到达 except 子句,try..except 也确实很慢.

However, it is also true that try..except is slow if the except clause is often being reached.

有没有办法对该块进行编码,以便不引发异常并使用 try..except 来捕获不太频繁的情况?

Is there a way to code that block so that an exception is not being raised and use try..except to catch the less frequent condition?

或者如果不是,为了效率,你可以选择不使用try..except.编程中几乎没有硬性规则.您必须根据您的顾虑平衡选择自己的方式.

Or if not, for the sake of efficiency, you may choose not to use try..except. There are few hard and fast rules in programming. You have to choose your way based on your balance of concerns.

这篇关于Python——捕获异常的效率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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