<legend id='zUr0f'><style id='zUr0f'><dir id='zUr0f'><q id='zUr0f'></q></dir></style></legend>

    <tfoot id='zUr0f'></tfoot>
      <i id='zUr0f'><tr id='zUr0f'><dt id='zUr0f'><q id='zUr0f'><span id='zUr0f'><b id='zUr0f'><form id='zUr0f'><ins id='zUr0f'></ins><ul id='zUr0f'></ul><sub id='zUr0f'></sub></form><legend id='zUr0f'></legend><bdo id='zUr0f'><pre id='zUr0f'><center id='zUr0f'></center></pre></bdo></b><th id='zUr0f'></th></span></q></dt></tr></i><div id='zUr0f'><tfoot id='zUr0f'></tfoot><dl id='zUr0f'><fieldset id='zUr0f'></fieldset></dl></div>
    1. <small id='zUr0f'></small><noframes id='zUr0f'>

      • <bdo id='zUr0f'></bdo><ul id='zUr0f'></ul>

      1. 是否有内置或更多 Pythonic 方式来尝试将字符串解

        时间:2023-09-11

          <i id='1UA61'><tr id='1UA61'><dt id='1UA61'><q id='1UA61'><span id='1UA61'><b id='1UA61'><form id='1UA61'><ins id='1UA61'></ins><ul id='1UA61'></ul><sub id='1UA61'></sub></form><legend id='1UA61'></legend><bdo id='1UA61'><pre id='1UA61'><center id='1UA61'></center></pre></bdo></b><th id='1UA61'></th></span></q></dt></tr></i><div id='1UA61'><tfoot id='1UA61'></tfoot><dl id='1UA61'><fieldset id='1UA61'></fieldset></dl></div>
          <legend id='1UA61'><style id='1UA61'><dir id='1UA61'><q id='1UA61'></q></dir></style></legend>

          • <small id='1UA61'></small><noframes id='1UA61'>

            • <bdo id='1UA61'></bdo><ul id='1UA61'></ul>
              <tfoot id='1UA61'></tfoot>

                <tbody id='1UA61'></tbody>

                  本文介绍了是否有内置或更多 Pythonic 方式来尝试将字符串解析为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  在尝试将字符串解析为整数时,我必须编写以下函数才能正常失败.我想 Python 有内置的东西可以做到这一点,但我找不到它.如果没有,是否有一种更 Pythonic 的方式,不需要单独的函数?

                  I had to write the following function to fail gracefully when trying to parse a string to an integer. I would imagine Python has something built in to do this, but I can't find it. If not, is there a more Pythonic way of doing this that doesn't require a separate function?

                  def try_parse_int(s, base=10, val=None):
                    try:
                      return int(s, base)
                    except ValueError:
                      return val
                  

                  我最终使用的解决方案是修改了@sharjeel 的答案.以下内容在功能上相同,但我认为更具可读性.

                  The solution I ended up using was a modification of @sharjeel's answer. The following is functionally identical, but, I think, more readable.

                  def ignore_exception(exception=Exception, default_val=None):
                    """Returns a decorator that ignores an exception raised by the function it
                    decorates.
                  
                    Using it as a decorator:
                  
                      @ignore_exception(ValueError)
                      def my_function():
                        pass
                  
                    Using it as a function wrapper:
                  
                      int_try_parse = ignore_exception(ValueError)(int)
                    """
                    def decorator(function):
                      def wrapper(*args, **kwargs):
                        try:
                          return function(*args, **kwargs)
                        except exception:
                          return default_val
                      return wrapper
                    return decorator
                  

                  推荐答案

                  这是一个非常常规的场景,所以我编写了一个ignore_exception"装饰器,它适用于各种抛出异常而不是优雅地失败的函数:

                  This is a pretty regular scenario so I've written an "ignore_exception" decorator that works for all kinds of functions which throw exceptions instead of failing gracefully:

                  def ignore_exception(IgnoreException=Exception,DefaultVal=None):
                      """ Decorator for ignoring exception from a function
                      e.g.   @ignore_exception(DivideByZero)
                      e.g.2. ignore_exception(DivideByZero)(Divide)(2/0)
                      """
                      def dec(function):
                          def _dec(*args, **kwargs):
                              try:
                                  return function(*args, **kwargs)
                              except IgnoreException:
                                  return DefaultVal
                          return _dec
                      return dec
                  

                  在您的情况下的用法:

                  sint = ignore_exception(ValueError)(int)
                  print sint("Hello World") # prints none
                  print sint("1340") # prints 1340
                  

                  这篇关于是否有内置或更多 Pythonic 方式来尝试将字符串解析为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Python:假与 0 下一篇:反转 Python 整数的位

                  相关文章

                  最新文章

                  • <bdo id='CAseD'></bdo><ul id='CAseD'></ul>
                  1. <i id='CAseD'><tr id='CAseD'><dt id='CAseD'><q id='CAseD'><span id='CAseD'><b id='CAseD'><form id='CAseD'><ins id='CAseD'></ins><ul id='CAseD'></ul><sub id='CAseD'></sub></form><legend id='CAseD'></legend><bdo id='CAseD'><pre id='CAseD'><center id='CAseD'></center></pre></bdo></b><th id='CAseD'></th></span></q></dt></tr></i><div id='CAseD'><tfoot id='CAseD'></tfoot><dl id='CAseD'><fieldset id='CAseD'></fieldset></dl></div>

                    <small id='CAseD'></small><noframes id='CAseD'>

                    1. <tfoot id='CAseD'></tfoot><legend id='CAseD'><style id='CAseD'><dir id='CAseD'><q id='CAseD'></q></dir></style></legend>