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

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

      1. <legend id='XnSNr'><style id='XnSNr'><dir id='XnSNr'><q id='XnSNr'></q></dir></style></legend>
      2. 在 Python 中自省构造函数 __init__ 的参数

        Introspecting arguments from the constructor function __init__ in Python(在 Python 中自省构造函数 __init__ 的参数)

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

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

              <tbody id='wKdRX'></tbody>
              • <bdo id='wKdRX'></bdo><ul id='wKdRX'></ul>

                  本文介绍了在 Python 中自省构造函数 __init__ 的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  什么是从 __init__ 中提取参数而不创建新实例的方法.代码示例:

                  What is a way to extract arguments from __init__ without creating new instance. The code example:

                  class Super:
                      def __init__(self, name):
                          self.name = name
                  

                  我正在寻找类似 Super.__dict__.keys() 类型的解决方案.只是为了检索名称参数信息而不添加任何值.有这样的选择吗?

                  I am looking something like Super.__dict__.keys()type solution. Just to retrieve name argument information without adding any values. Is there such an option to do that?

                  推荐答案

                  Python 3.3+ 更新(如 beeb 在评论中)

                  Update for Python 3.3+ (as pointed out by beeb in the comments)

                  您可以使用 在 Python 3.3 中引入的 inspect.signature:

                  You can use inspect.signature introduced in Python 3.3:

                  class Super:
                      def __init__(self, name, kwarg='default'):
                          print('instantiated')
                          self.name = name
                  
                  >>> import inspect
                  >>> inspect.signature(Super.__init__)
                  <Signature (self, name, kwarg='default')>
                  

                  原答案如下

                  您可以使用 inspect

                  >>> import inspect
                  >>> inspect.getargspec(Super.__init__)
                  ArgSpec(args=['self', 'name'], varargs=None, keywords=None, defaults=None)
                  >>> 
                  

                  inspect.getargspec 实际上并没有创建 Super 的实例,见下文:

                  inspect.getargspec doesn't actually create an instance of Super, see below:

                  import inspect
                  
                  class Super:
                      def __init__(self, name):
                          print 'instantiated'
                          self.name = name
                  
                  print inspect.getargspec(Super.__init__)
                  

                  这个输出:

                  ### Run test.a ###
                  ArgSpec(args=['self', 'name'], varargs=None, keywords=None, defaults=None)
                  >>> 
                  

                  请注意,instantiated 从未被打印出来.

                  Note that instantiated never got printed.

                  这篇关于在 Python 中自省构造函数 __init__ 的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Adding config modes to Plotly.Py offline - modebar(将配置模式添加到 Plotly.Py 离线 - 模式栏)
                  Plotly: How to style a plotly figure so that it doesn#39;t display gaps for missing dates?(Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙?)
                  python save plotly plot to local file and insert into html(python将绘图保存到本地文件并插入到html中)
                  Plotly: What color cycle does plotly express follow?(情节:情节表达遵循什么颜色循环?)
                  How to save plotly express plot into a html or static image file?(如何将情节表达图保存到 html 或静态图像文件中?)
                  Plotly: How to make a line plot from a pandas dataframe with a long or wide format?(Plotly:如何使用长格式或宽格式的 pandas 数据框制作线图?)
                    <bdo id='QwiUR'></bdo><ul id='QwiUR'></ul>
                        <tbody id='QwiUR'></tbody>

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

                            <tfoot id='QwiUR'></tfoot>

                          • <small id='QwiUR'></small><noframes id='QwiUR'>