Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python3中调用super()的4种方法中,使用哪一种?_Python_Oop_Python 3.x_Super - Fatal编程技术网

在Python3中调用super()的4种方法中,使用哪一种?

在Python3中调用super()的4种方法中,使用哪一种?,python,oop,python-3.x,super,Python,Oop,Python 3.x,Super,我想知道什么时候使用Python 3()的什么风格 模块内置类super的帮助: 超级类(对象) |super()->与super(类)相同 |超级(类型)->未绑定的超级对象 |super(type,obj)->绑定的super对象;需要iInstance(对象,类型) |super(type,type2)->绑定的super对象;需要issubclass(类型2,类型) 到目前为止,我只在没有参数的情况下使用了super(),并且它按照预期工作(由Java开发人员) 问题: 在这种情况下

我想知道什么时候使用Python 3()的什么风格

模块内置类super的帮助:
超级类(对象)
|super()->与super(类)相同
|超级(类型)->未绑定的超级对象
|super(type,obj)->绑定的super对象;需要iInstance(对象,类型)
|super(type,type2)->绑定的super对象;需要issubclass(类型2,类型)
到目前为止,我只在没有参数的情况下使用了
super()
,并且它按照预期工作(由Java开发人员)

问题:

  • 在这种情况下,“绑定”是什么意思
  • 绑定和未绑定的超级对象之间有什么区别
  • 何时使用
    super(type,obj)
    以及何时使用
    super(type,type2)
  • 像在
    Mother.\uuu init\uuuu(…)
    中那样命名超类会更好吗

    • 让我们使用以下类进行演示:

      class A(object):
          def m(self):
              print('m')
      
      class B(A): pass
      
      未绑定的
      super
      对象不向类分配属性访问,您必须使用描述符协议:

      >>> super(B).m
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
      AttributeError: 'super' object has no attribute 'm'
      >>> super(B).__get__(B(), B)
      <super: <class 'B'>, <B object>>
      
      super
      绑定到类的对象提供函数(Python 2中的未绑定方法):

      超级(B,B).m >>>super(B,B).m() 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 TypeError:m()正好接受1个位置参数(给定0) >>>super(B,B).m(B()) M
      请参阅Michele Simionato的“关于Python Super的知识”博客文章系列(,)以了解更多信息

      简要说明,
      Super
      的新用法是在Python 3.0中实现的。特别相关的

      super().foo(1, 2)
      
      要替换旧的:

      super(Foo, self).foo(1, 2)
      

      这个问题是关于Python3的,但是Simionato的博客文章系列是关于Python2的,并且提到它的优点是避免在调用语法中重复类的名称,因为这个名称隐藏在私有名称的混乱机制中。。这在Python3中不再适用,因此至少有一个优势是过时的。这回答了OP的“每个都做了什么?”但它没有回答“你什么时候会使用每个?”也没有实际解释“未绑定”与“绑定”;如果
      B
      是类名,
      B=B()
      是实例,则
      B
      是未绑定对象,
      B
      是绑定对象。
      >>> super(B, B).m
      <function m at 0xb761482c>
      >>> super(B, B).m()
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
      TypeError: m() takes exactly 1 positional argument (0 given)
      >>> super(B, B).m(B())
      m
      
      super().foo(1, 2)
      
      super(Foo, self).foo(1, 2)