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

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

                  相关文档推荐

                  groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
                  Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
                  Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)
                  Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)
                  Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)
                  Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)

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