如何在Python中编写一个空的缩进块?

发布于 2024-08-07 09:31:52 字数 332 浏览 7 评论 0原文

我有一些代码,例如:

try:
    do_the_first_part()
except SomeError:
do_the_next_part()

我收到一个错误,上面写着“预期有一个缩进块” - 但我不想在我的 except 块中写入任何内容,我只是希望它捕获并吞下异常。我怎样才能在Python中做到这一点?


另请参阅: 如何使用“pass”语句?

I have some code like:

try:
    do_the_first_part()
except SomeError:
do_the_next_part()

I get an error that says "expected an indented block" - but I don't want to write anything inside my except block, I just want it to catch and swallow the exception. How can I do this in Python?


See also: How to use "pass" statement?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

睫毛溺水了 2024-08-14 09:31:52

编写即可

pass

只需按照编辑中的方式

try:
    # Do something illegal.
    ...
except:
    # Pretend nothing happened.
    pass

:@swillden 提出了一个很好的观点,即,总的来说,这是一个糟糕的想法。您至少应该说出

except TypeError, DivideByZeroError:

您想要处理的任何类型的错误。否则你可能会掩盖更大的问题。

Just write

pass

as in

try:
    # Do something illegal.
    ...
except:
    # Pretend nothing happened.
    pass

EDIT: @swillden brings up a good point, viz., this is a terrible idea in general. You should, at the least, say

except TypeError, DivideByZeroError:

or whatever kinds of errors you want to handle. Otherwise you can mask bigger problems.

書生途 2024-08-14 09:31:52

对于那些非常不清楚为什么要这样做的人。这是一个例子,我最初认为空块是个好主意:

def set_debug_dir(self, debug_dir=None):
    if debug_dir is None:
        debug_dir = self.__debug_dir
    elif isinstance(debug_dir, (Path, str)):
        debug_dir = debug_dir # this is my null operation
    elif isinstance(debug_dir, list):
        debug_dir = functools.reduce(os.path.join, debug_dir)
    else:
        raise TypeError('Unexpected type for debug_dir: {}'.format(type(debug_dir).__name__))

但重新组织语句会更清晰:

def set_debug_dir(self, debug_dir=None):
    if debug_dir is None:
        debug_dir = self.__debug_dir
    elif isinstance(debug_dir, list):
        debug_dir = functools.reduce(os.path.join, debug_dir)
    elif not isinstance(debug_dir, (Path, str)):
        raise TypeError('Unexpected type for debug_dir: {}'.format(type(debug_dir).__name__))

For those who are very unclear as to why you would want to do this. Here is an example where I initially thought that an empty block would be a good idea:

def set_debug_dir(self, debug_dir=None):
    if debug_dir is None:
        debug_dir = self.__debug_dir
    elif isinstance(debug_dir, (Path, str)):
        debug_dir = debug_dir # this is my null operation
    elif isinstance(debug_dir, list):
        debug_dir = functools.reduce(os.path.join, debug_dir)
    else:
        raise TypeError('Unexpected type for debug_dir: {}'.format(type(debug_dir).__name__))

But it would be more clear to reorganize the statement:

def set_debug_dir(self, debug_dir=None):
    if debug_dir is None:
        debug_dir = self.__debug_dir
    elif isinstance(debug_dir, list):
        debug_dir = functools.reduce(os.path.join, debug_dir)
    elif not isinstance(debug_dir, (Path, str)):
        raise TypeError('Unexpected type for debug_dir: {}'.format(type(debug_dir).__name__))
剧终人散尽 2024-08-14 09:31:52

我从来没有在更永久的代码中这样做过,但我经常将其作为占位符,

if some_expression:
  True
else:
  do_something(blah)

只需在其中粘贴 True 即可停止错误。不确定这是否有什么不好的地方。

I've never done this in more permanent code, but I frequently do it as a placeholder

if some_expression:
  True
else:
  do_something(blah)

Just sticking a True in there will stop the error. Not sure if there's anything bad about this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文