是否“继续”Pythonic方式逃离try catch块?

时间:2022-06-02 22:19:57

I am new to django and thought of doing to simple django application to learn more about it, In one of the places in code I had to pick locationName and getting elements that matched same id as locationName in a table. When I started wondering is continue the most pythonic way to escape a for-loop?

我是django的新手,想到了简单的django应用程序来了解它,在代码中的一个地方我必须选择locationName并获取与tableName中的locationName匹配相同id的元素。当我开始想知道是继续以最pythonic的方式逃避for循环?

Code in question is given below :

有问题的代码如下:

for locationName in locationGroup:
    idRef = locationName.id
    try:
        element = location.objects.order_by('-id').filter(name__id=idRef)[0]
    except IndexError:
        continue

4 个解决方案

#1


8  

If there's some code you don't want getting executed after the except clause, continue is perfectly valid, otherwise some might find pass more suitable.

如果有一些代码你不希望在except子句之后执行,则continue完全有效,否则有些代码可能会找到更合适的代码。

for x in range(y):
    try:
        do_something()
    except SomeException:
        continue
    # The following line will not get executed for the current x value if a SomeException is raised
    do_another_thing() 

for x in range(y):
    try:
        do_something()
    except SomeException:
        pass
    # The following line will get executed regardless of whether SomeException is thrown or not
    do_another_thing() 

#2


3  

That's exactly what the continue/break keywords are for, so yes, that's the simplest and most pythonic way of doing it.

这正是continue / break关键字的用途,所以是的,这是最简单,最pythonic的方式。

There should be one-- and preferably only one --obvious way to do it.

应该有一个 - 最好只有一个 - 显而易见的方法。

#3


2  

You should use

你应该用

try:
    element = location.objects.order_by('-id').filter(name__id=idRef)[0]
except IndexError:
    pass

#4


1  

You make it a bit hard to tell what you're doing. The code simply checks if you get any rows from the query, by looking at the first element and catching the IndexError.

你要说你在做什么有点难。代码只是通过查看第一个元素并捕获IndexError来检查是否从查询中获取任何行。

I would write it in a way that makes this intention much clearer:

我会以一种使这个意图更加清晰的方式来编写它:

for locationName in locationGroup:
    idRef = locationName.id
    rows = location.objects.order_by('-id').filter(name__id=idRef)
    if rows: # if we have rows do stuff otherwise continue
         element = rows[0]
         ...

In this case you can use get which makes it even more clearer:

在这种情况下,您可以使用get,这使得它更加清晰:

for locationName in locationGroup:
    idRef = locationName.id
    try:
         element = location.objects.get(name__id=idRef)
    except location.DoesNotExist:
         pass

#1


8  

If there's some code you don't want getting executed after the except clause, continue is perfectly valid, otherwise some might find pass more suitable.

如果有一些代码你不希望在except子句之后执行,则continue完全有效,否则有些代码可能会找到更合适的代码。

for x in range(y):
    try:
        do_something()
    except SomeException:
        continue
    # The following line will not get executed for the current x value if a SomeException is raised
    do_another_thing() 

for x in range(y):
    try:
        do_something()
    except SomeException:
        pass
    # The following line will get executed regardless of whether SomeException is thrown or not
    do_another_thing() 

#2


3  

That's exactly what the continue/break keywords are for, so yes, that's the simplest and most pythonic way of doing it.

这正是continue / break关键字的用途,所以是的,这是最简单,最pythonic的方式。

There should be one-- and preferably only one --obvious way to do it.

应该有一个 - 最好只有一个 - 显而易见的方法。

#3


2  

You should use

你应该用

try:
    element = location.objects.order_by('-id').filter(name__id=idRef)[0]
except IndexError:
    pass

#4


1  

You make it a bit hard to tell what you're doing. The code simply checks if you get any rows from the query, by looking at the first element and catching the IndexError.

你要说你在做什么有点难。代码只是通过查看第一个元素并捕获IndexError来检查是否从查询中获取任何行。

I would write it in a way that makes this intention much clearer:

我会以一种使这个意图更加清晰的方式来编写它:

for locationName in locationGroup:
    idRef = locationName.id
    rows = location.objects.order_by('-id').filter(name__id=idRef)
    if rows: # if we have rows do stuff otherwise continue
         element = rows[0]
         ...

In this case you can use get which makes it even more clearer:

在这种情况下,您可以使用get,这使得它更加清晰:

for locationName in locationGroup:
    idRef = locationName.id
    try:
         element = location.objects.get(name__id=idRef)
    except location.DoesNotExist:
         pass