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

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

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

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

        <tfoot id='UEFQu'></tfoot>

        将一长串常量导入 Python 文件

        Importing a long list of constants to a Python file(将一长串常量导入 Python 文件)
        <legend id='vUmSM'><style id='vUmSM'><dir id='vUmSM'><q id='vUmSM'></q></dir></style></legend>
      2. <i id='vUmSM'><tr id='vUmSM'><dt id='vUmSM'><q id='vUmSM'><span id='vUmSM'><b id='vUmSM'><form id='vUmSM'><ins id='vUmSM'></ins><ul id='vUmSM'></ul><sub id='vUmSM'></sub></form><legend id='vUmSM'></legend><bdo id='vUmSM'><pre id='vUmSM'><center id='vUmSM'></center></pre></bdo></b><th id='vUmSM'></th></span></q></dt></tr></i><div id='vUmSM'><tfoot id='vUmSM'></tfoot><dl id='vUmSM'><fieldset id='vUmSM'></fieldset></dl></div>
          <tbody id='vUmSM'></tbody>

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

          <tfoot id='vUmSM'></tfoot>

                  <bdo id='vUmSM'></bdo><ul id='vUmSM'></ul>
                  本文介绍了将一长串常量导入 Python 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在 Python 中,是否有类似的 C 预处理器语句,例如?:

                  In Python, is there an analogue of the C preprocessor statement such as?:

                  #define MY_CONSTANT 50

                  另外,我有一大串常量我想导入到几个类中.有没有类似的方法在 .py 文件中将常量声明为像上面这样的一长串语句并将其导入另一个 .py 文件?

                  Also, I have a large list of constants I'd like to import to several classes. Is there an analogue of declaring the constants as a long sequence of statements like the above in a .py file and importing it to another .py file?

                  编辑.

                  文件 Constants.py 内容如下:

                  #!/usr/bin/env python
                  # encoding: utf-8
                  """
                  Constants.py
                  """
                  
                  MY_CONSTANT_ONE = 50
                  MY_CONSTANT_TWO = 51
                  

                  myExample.py 内容为:

                  #!/usr/bin/env python
                  # encoding: utf-8
                  """
                  myExample.py
                  """
                  
                  import sys
                  import os
                  
                  import Constants
                  
                  class myExample:
                      def __init__(self):
                          self.someValueOne = Constants.MY_CONSTANT_ONE + 1
                          self.someValueTwo = Constants.MY_CONSTANT_TWO + 1
                  
                  if __name__ == '__main__':
                      x = MyClass()
                  

                  编辑.

                  来自编译器,

                  NameError: "全局名称'MY_CONSTANT_ONE' 未定义"

                  NameError: "global name 'MY_CONSTANT_ONE' is not defined"

                  function init in myExample at line13 self.someValueOne =常量.MY_CONSTANT_ONE + 1 份输出程序以代码 #1 退出0.06 秒后.

                  function init in myExample at line 13 self.someValueOne = Constants.MY_CONSTANT_ONE + 1 copy output Program exited with code #1 after 0.06 seconds.

                  推荐答案

                  Python 没有经过预处理.你可以只创建一个文件 myconstants.py:

                  Python isn't preprocessed. You can just create a file myconstants.py:

                  MY_CONSTANT = 50
                  

                  导入它们就可以了:

                  import myconstants
                  print myconstants.MY_CONSTANT * 2
                  

                  这篇关于将一长串常量导入 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 数据帧进行分组)
                    <legend id='JQ9Tw'><style id='JQ9Tw'><dir id='JQ9Tw'><q id='JQ9Tw'></q></dir></style></legend>

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

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

                              <tbody id='JQ9Tw'></tbody>