相关疑难解决方法(0)

Python3 - TypeError:module .__ init __()最多需要2个参数(给定3个)

请不要标记为重复,其他类似的问题没有解决我的问题.

这是我的设置

/main.py
/actions/ListitAction.py
/actions/ViewAction.py
Run Code Online (Sandbox Code Playgroud)

Main.py:

from actions import ListitAction, ViewAction
Run Code Online (Sandbox Code Playgroud)

ListitAction.py:

class ListitAction(object):    

    def __init__(self):        
        #some init behavior

    def build_uri():
        return "test.uri"
Run Code Online (Sandbox Code Playgroud)

ViewAction.py

from actions import ListitAction

class ViewAction(ListitAction):

    def __init__(self, view_id):
        ListitAction.__init__(self)
        self.view_id = view_id

    def build_uri():
        return "test"
Run Code Online (Sandbox Code Playgroud)

运行:

$ python3 main.py
Run Code Online (Sandbox Code Playgroud)

我收到的唯一错误消息是:

Traceback (most recent call last):
  File "/home/jlevac/workspace/project/listit.py", line 11, in <module>
    from actions import ListitAction, ViewAction, CommentsAction
  File "/home/jlevac/workspace/project/actions/ViewAction.py", line 3, in <module>
    class ViewAction(ListitAction):
TypeError: module.__init__() takes at most 2 arguments (3 given) …
Run Code Online (Sandbox Code Playgroud)

python python-3.x

9
推荐指数
1
解决办法
2万
查看次数

Python中的super().__ init __()和显式超类__init __()之间的行为差​​异

super().__init__()在代码中使用和显式调用超类构造函数之间的行为有一个无法解释的差异.

class IPElement(object):

def __init__(self, ip_type='IPv4'):
    self.ip_type = ip_type

class IPAddressSimple(IPElement):

    def __init__(self, ip_name, ip_type='IPv4'):
        self.ip_name = ip_name
        super().__init__(self, ip_type=ip_type)
Run Code Online (Sandbox Code Playgroud)

这里,该行super().__init__(self, ip_type=ip_type)导致类型错误:

TypeError: __init__() got multiple values for argument 'ip_type'
Run Code Online (Sandbox Code Playgroud)

当我将调用更改为ip_type按位置传递时(例如,super().__init__(self, ip_type)我得到一个不同的类型错误:

TypeError: __init__() takes from 1 to 2 positional arguments but 3 were given
Run Code Online (Sandbox Code Playgroud)

这些错误都没有对我有意义,当我用super()超类的显式名称替换时,一切都按预期工作.以下工作正常,就像按位置传递ip_type一样:

class IPAddressSimple(IPElement):

        def __init__(self, ip_name, ip_type='IPv4'):
            self.ip_name = ip_name
            IPElement.__init__(self, ip_type=ip_type)
Run Code Online (Sandbox Code Playgroud)

__init__如果有必要,我当然可以使用显式调用超类方法,但我想理解为什么super()不在这里工作.

inheritance super keyword-argument python-3.x

7
推荐指数
1
解决办法
517
查看次数

当直接从`object`继承时,我应该调用super().__ init __()吗?

因为这个问题是关于继承的super,所以我们先写一个类.这是一个代表一个人的简单日常课程:

class Person:
    def __init__(self, name):
        super().__init__()

        self.name = name
Run Code Online (Sandbox Code Playgroud)

像每个好类一样,它在初始化之前调用它的父构造函数.这个班级完美地完成了它的工作; 它可以毫无问题地使用:

>>> Person('Tom')
<__main__.Person object at 0x7f34eb54bf60>
Run Code Online (Sandbox Code Playgroud)

但是当我尝试创建一个继承自两个Person类和另一个类的类时,事情突然出现了:

class Horse:
    def __init__(self, fur_color):
        super().__init__()

        self.fur_color = fur_color

class Centaur(Person, Horse):
    def __init__(self, name, fur_color):
        # ??? now what?
        super().__init__(name)  # throws TypeError: __init__() missing 1 required positional argument: 'fur_color'
        Person.__init__(self, name)  # throws the same error
Run Code Online (Sandbox Code Playgroud)

由于钻石继承(object顶部的类),因此无法Centaur正确初始化实例.将super().__init__()Person结束了通话Horse.__init__,因为它抛出一个异常fur_color参数丢失.

但是如果PersonHorse不打电话,这个问题就不会存在super().__init__() …

python inheritance super

7
推荐指数
2
解决办法
755
查看次数

方法解析顺序 (MRO) 在此 Python 代码中如何工作

class parent:
    def __init__(self):
        self.a=2
        self.b=4
    def form1(self): 
        print("calling parent from1")
        print('p',self.a+self.b)
 
class child1(parent):
    def __init__(self):
        self.a=50
        self.b=4
    def form1(self):
        print('bye',self.a-self.b)
    def callchildform1(self):
        print("calling parent from child1")
        super().form1()
 
class child2(parent):
    def __init__(self):
        self.a=3
        self.b=4
    def form1(self):
        print('hi',self.a*self.b)
    def callchildform1(self):
        print("calling parent from child2")
        super().form1()
 
class grandchild(child1,child2):
    def __init__(self):
        self.a=10
        self.b=4
    def callingparent(self):
        super().form1()
 
g=grandchild()
g.callchildform1()
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,当我调用 时g.callchildform1(),根据 MRO 规则,将首先在同一个类中搜索该方法,然后是第一个父级(此处为 child1),然后是第二个父级(child2)。正如预期的那样,它调用child1.callchildform1()并执行第一行print("calling parent from child1")super().form1()但在此之后,我预计将执行下一行,该行将被调用parent.form1(),但这种情况不会发生。相反,child2.form1()正在被调用。请解释一下为什么会出现这种情况?

python oop multiple-inheritance super

5
推荐指数
1
解决办法
1336
查看次数

python变量继承如何工作

我正在尝试一些非常基本的Python继承:

import os

class Parent:
    def __init__(self):
        self.text = 'parent'

    def getText(self):
        print self.text

class Child1(Parent):
    def __init__(self):
        self.x = 'x'

class Child2(Parent):
    def __init__(self):
        self.x = 'x'

if __name__ == "__main__": 
    parent = Parent()
    child1 = Child1()
    child2 = Child2()

    parent.getText()
    child1.getText()
    child2.getText()
Run Code Online (Sandbox Code Playgroud)

但我一直在

Child1实例没有属性'text'

如何将变量传递给孩子?(我有Java/C#的背景,没有太多的Python)

python inheritance

4
推荐指数
1
解决办法
4976
查看次数

在python3中使用没有继承类的super

有人知道下面的代码意味着什么super(xxx, self).__init__()吗?它有什么作用?我虽然它应该是 ABC 类继承自其他类,但我没有看到继承自哪个类。

class ABC:

    def __init__(self):
        super(ABC, self).__init__()
Run Code Online (Sandbox Code Playgroud)

python python-3.x

4
推荐指数
1
解决办法
381
查看次数

如何使用super()初始化python中的子类参数

我已经看到了关于如何在python中使用'super'的各种示例,但在所有这些示例中,没有参数传递给子类的init方法.请考虑以下示例.

这是基类:

class Animal(object):

    def __init__(self, genus):
        self.genus = genus
Run Code Online (Sandbox Code Playgroud)

现在heres没有使用超级:

class Dog(Animal):

    def __init__(self, genus):
        Animal.__init__(self, genus)


x = Dog('Canis')
print x.genus  # Successfully prints "Canis"
Run Code Online (Sandbox Code Playgroud)

现在,当我使用super时:

class Dog(Animal):

    def __init__(self, genus):
        super(Animal, self).__init__(genus)

x = Dog('Canis')
print x.genus  
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

TypeError: object.__init__() takes no parameters
Run Code Online (Sandbox Code Playgroud)

那么如果对象.init()不带参数,如何在实例化那个子类时设置这个特定动物的特定属?我是否必须显式分配这样的变量:

class Dog(Animal):

    def __init__(self, genus):
        super(Animal, self).__init__()
        self.genus = genus
Run Code Online (Sandbox Code Playgroud)

python inheritance super

3
推荐指数
1
解决办法
3057
查看次数

PySide:'PySide.QtCore.Signal'对象没有属性'emit'

使用以下代码,我'PySide.QtCore.Signal' object has no attribute 'emit'在尝试发出信号时得到一个错误():

#!/usr/bin/env python

from PySide import QtCore

class TestSignalClass(QtCore.QObject):
    somesignal = QtCore.Signal()

    def speak_me(self):
        self.speak.emit()
    def __init__(self):
        try:
            self.somesignal.emit()
        except Exception as e:
            print("__init__:")
            print(e)

t = TestSignalClass()
Run Code Online (Sandbox Code Playgroud)

我该怎么做才能解决这个问题?

python qt pyside

2
推荐指数
1
解决办法
3625
查看次数

super(MyObject, self).__init__() 在类 MyObject __init__() 函数中做什么?

class MyObject1(object):
    def __init__(self):
        super(MyObject1, self).__init__()
        pass

class MyObject2(object):
    def __init__(self, arg):
        super(MyObject2, self).__init__()
        pass
Run Code Online (Sandbox Code Playgroud)

我读过这样的python27代码,

我知道“超级”是指父类构造函数,

但我不明白为什么这两个类称自己为'构造函数' __init__',

好像没什么实际效果。

python inheritance super

2
推荐指数
1
解决办法
1万
查看次数

理解python的超级方法,为什么D().test()将返回'B-> C'而不是'B - > A'

我在这里看了关于python的super()方法的其他问题,但我仍然觉得很难理解整个概念.

我也在查看pro python一书中的例子

引用的例子是

class A(object):
     def test(self):
         return 'A'

class B(A):
     def test(self):
         return 'B-->' + super(B, self).test()

class C(A):
     def test(self):
         return 'C'

class D(B, C):
      pass

>>> A().test()
'A'
>>> B().test()
'B-->A'
>>> C().test()
'C'
>>> D().test()
'B-->C'

>>> A.__mro__
(__main__.A, object)

>>> B.__mro__
(__main__.B, __main__.A, object)

>>> C.__mro__
(__main__.C, __main__.A, object)

>>> D.__mro__
(__main__.D, __main__.B, __main__.C, __main__.A, object)
Run Code Online (Sandbox Code Playgroud)

为什么要做D().test()我们得到'B - > C'而不是'B - > A'的输出

书中的解释是

在最常见的情况下,包括此处显示的用法,super()接受两个参数:类和该类的实例.正如我们在此处的示例所示,实例对象确定将使用哪个MRO来解析结果对象上的任何属性.提供的类确定该MRO的子集,因为super()仅使用在提供的类之后发生的MRO中的那些条目.

我仍然觉得解释有点难以理解.这可能是一个可能的重复,并且已经多次询问与此类似的问题,但如果我理解了这一点,我可能能够更好地理解其他问题.

使用__init __()方法理解Python super()

"超级"在Python中做了什么?

python,继承,super()方法

[python]:被super()搞糊涂了

python super

1
推荐指数
1
解决办法
445
查看次数