Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在使用空/缺少/未初始化的对象/属性时,最优雅的python处理方式_Python_Error Handling - Fatal编程技术网

在使用空/缺少/未初始化的对象/属性时,最优雅的python处理方式

在使用空/缺少/未初始化的对象/属性时,最优雅的python处理方式,python,error-handling,Python,Error Handling,表单可能不存在,而仰慕者或模型可能不存在于表单中 处理这种情况最优雅的方法是什么?您可以使用,将sentinel值作为默认值传递: if form.admirer_or_model == "model": model_form_css_style = None 在Python实现之前,上述操作将一直有效。您可以使用,将sentinel值作为默认值传递: if form.admirer_or_model == "model": model_form_css_style = Non

表单可能不存在,而仰慕者或模型可能不存在于表单中

处理这种情况最优雅的方法是什么?

您可以使用,将sentinel值作为默认值传递:

if form.admirer_or_model == "model":
    model_form_css_style = None
在Python实现之前,上述操作将一直有效。

您可以使用,将sentinel值作为默认值传递:

if form.admirer_or_model == "model":
    model_form_css_style = None

在Python实现之前,以上操作都可以完成。

有许多可能的选择。哪种方法最方便,通常取决于如果发生错误,代码流应该改变多少

  • 如果流被完全删除-即功能刚刚中断

    • 在这两种情况下,不执行任何操作都会引发
      AttributeError

      if getattr(form, 'admirer_or_model', form) == "model":
          ...
      
    • 在下一个建议中检查,但没有
      else
      子句:

      try: if form.admirer_or_model == "model":
          <...>
      except AttributeError as e:
          <handle the error and quit, e.g.>
          raise TypeError("`form': " + e.message)    # a common way to signal that
                                                     #an argument failed a type and/or
                                                     #a duck test
      
    • 处理错误,但不要退出-特别是如果两个错误的处理都是常见的:

      if form and hasattr(form,'admirer_or_model'):
          <normal case>
      else:
          <alternate case>
      <proceed further>
      
      尝试:如果form.adminer\u或\u model==“model”:
      除属性错误为e外:
      
  • 如果流根本没有变化,即您只需提供一个默认值

    • 使用三元/其他默认提供的构造-例如,如果form else和
      getattr(form,'adminer\u或'u model')

      if getattr(
      (如表格另有规定,则填写表格),
      “仰慕者”或“模特儿”,)=“模特儿”:
      


还请注意,其中一些构造将错误处理块放在
if
块之前,并带有
==“model”
和一些-之后。如果
If
块很大,这可能会影响可读性:错误处理块最好放在触发错误的行附近。

有许多可能的选择。哪种方法最方便,通常取决于如果发生错误,代码流应该改变多少

  • 如果流被完全删除-即功能刚刚中断

    • 在这两种情况下,不执行任何操作都会引发
      AttributeError

      if getattr(form, 'admirer_or_model', form) == "model":
          ...
      
    • 在下一个建议中检查,但没有
      else
      子句:

      try: if form.admirer_or_model == "model":
          <...>
      except AttributeError as e:
          <handle the error and quit, e.g.>
          raise TypeError("`form': " + e.message)    # a common way to signal that
                                                     #an argument failed a type and/or
                                                     #a duck test
      
    • 处理错误,但不要退出-特别是如果两个错误的处理都是常见的:

      if form and hasattr(form,'admirer_or_model'):
          <normal case>
      else:
          <alternate case>
      <proceed further>
      
      尝试:如果form.adminer\u或\u model==“model”:
      除属性错误为e外:
      
  • 如果流根本没有变化,即您只需提供一个默认值

    • 使用三元/其他默认提供的构造-例如,如果form else和
      getattr(form,'adminer\u或'u model')

      if getattr(
      (如表格另有规定,则填写表格),
      “仰慕者”或“模特儿”,)=“模特儿”:
      

还请注意,其中一些构造将错误处理块放在
if
块之前,并带有
==“model”
和一些-之后。如果
If
块很大,这可能会影响可读性:错误处理块最好放在触发错误的行附近。

也就是说,如果Python实现了安全导航。这是一个很大的“如果”,因为“大声”失败(引发异常并中断流)和“无声”失败(返回“空值”并继续流)是两个不同的基本选择,Python倾向于前者(Python Zen,koan 10:“错误永远不应该无声地通过”)。也就是说,如果Python实现了安全导航。这是一个很大的“如果”,因为“大声”失败(引发异常并中断流)和“无声”失败(返回“空值”并继续流)是两个不同的基本选择,Python倾向于前者(Python Zen,koan 10:“错误永远不应该无声地通过”)。