我是django的新手,想到了简单的django应用程序以了解它的更多信息,在代码中的一个地方我必须选择locationName并获得与locationName表中相同id的元素.当我开始想知道是continue逃避for循环的最pythonic方式?
有问题的代码如下:
for locationName in locationGroup:
idRef = locationName.id
try:
element = location.objects.order_by('-id').filter(name__id=idRef)[0]
except IndexError:
continue
Run Code Online (Sandbox Code Playgroud)
如果有一些代码你不想在except子句之后执行,那么它continue是完全有效的,否则有些代码可能会pass更合适.
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()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2347 次 |
| 最近记录: |