TypeError:super()参数1必须是类型,而不是Python3.7中的str

时间:2019-02-17 19:41:37

标签: python-3.x constructor

当我尝试在thermo.py的构造函数中调用超类的构造函数(以下文件)时,出现以下错误:

Traceback (most recent call last):
File "/var/wheaterstation/scripts/wsControl.py", line 58, in <module>
controller = controller()
File "/var/wheaterstation/scripts/wsControl.py", line 21, in __init__
self.thermo = thermo("Thermometer", logger)
File "/var/wheaterstation/scripts/thermo.py", line 7, in __init__
super(name, logger).__init__()
TypeError: super() argument 1 must be type, not str

我已经看到,在2.6中,如果超级类不扩展“对象”,通常会出现此错误。我试过了,但似乎没有用。

wsPart.py:

class wsPart(object):
logger = None
name = ""
def __init__(self, name, logger):
    self.logger = logger
    self.name = name
    self.logger.writeLog(self, "created")

def getName(self):
    return self.name

thermo.py:

import os, sys
from wsPart import wsPart
class thermo:
functional = False 
file = '/sys/bus/w1/devices/28-00000833e8ff/w1_slave'
def __init__(self, name, logger):
    super(name, logger).__init__() ## this calls the wsPart constructor
    functional = True

def read(self):
    ##reads out the file mentioned in line 5 and returns it
    fileobject = open(self.file)
    filecontent = fileobject.read()
    fileobject.close()
    self.logger.writeLog(self,"Completed Meassurement")
    return filecontent

和wsControl.py中重要的部分:

class controller():

name = ""
logger = None
thermo = None
dbConnector = None

def __init__(self):
    ##THis created the controller and all the other objects
    self.name = "Controller"
    ##Create Objects
    self.logger = logger()
    self.logger.writeLog(self, "logger created")
    self.thermo = thermo("Thermometer", logger)
    self.dbConnector = dbConnector("DBConnector",logger)

完整代码为here

1 个答案:

答案 0 :(得分:0)

我首先必须使Thermo成为wsPart的子类:

class thermo:

更改为

class thermo(wsPart):

然后必须更改:

super(name, logger).__init__()

收件人:

super().__init__(name, logger)