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

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

        Python 条件赋值运算符

        Python conditional assignment operator(Python 条件赋值运算符)

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

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

                  本文介绍了Python 条件赋值运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  Does a Python equivalent to the Ruby ||= operator ("set the variable if the variable is not set") exist?

                  Example in Ruby :

                   variable_not_set ||= 'bla bla'
                   variable_not_set == 'bla bla'
                  
                   variable_set = 'pi pi'
                   variable_set ||= 'bla bla'
                   variable_set == 'pi pi'
                  

                  解决方案

                  No, the replacement is:

                  try:
                     v
                  except NameError:
                     v = 'bla bla'
                  

                  However, wanting to use this construct is a sign of overly complicated code flow. Usually, you'd do the following:

                  try:
                     v = complicated()
                  except ComplicatedError: # complicated failed
                     v = 'fallback value'
                  

                  and never be unsure whether v is set or not. If it's one of many options that can either be set or not, use a dictionary and its get method which allows a default value.

                  这篇关于Python 条件赋值运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)

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

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

                      <legend id='DvJoo'><style id='DvJoo'><dir id='DvJoo'><q id='DvJoo'></q></dir></style></legend>
                        <tbody id='DvJoo'></tbody>

                            <tfoot id='DvJoo'></tfoot>