• <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 条件赋值运算符

        时间:2023-08-29

            • <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 条件赋值运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Python中的多个“或"条件 下一篇:在 Python 中分配类布尔值

                  相关文章

                  • <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>

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