Python 3.x TypeError:对象()不接受参数3

Python 3.x TypeError:对象()不接受参数3,python-3.x,Python 3.x,我是一个初学者,在尝试一个程序错误时:TypeError:object()不接受任何参数是经常发生的 next_output = True def start() : class Gate(object): """ class representing a gate. It can be any gate. """ def __init__(bit, *args): """ initialise the class """ b

我是一个初学者,在尝试一个程序错误时:TypeError:object()不接受任何参数是经常发生的

next_output = True
def start() :

    class Gate(object):

        """ class representing a gate. It can be any gate. """

    def __init__(bit, *args):
        """ initialise the class """
        bit.input = args
        bit.output = None

    def logic(bit):
        """ the intelligence to be performed """
        raise NotImplementedError

    def output(bit):
        """ the output of the gate """
        bit.logic()
        return bit.output

    a = int(input("Enter the first input: "))
    b = int(input("Enter the second input: "))

    class AndGate(Gate):
        """ class representing AND gate """

        def logic(bit):

            bit.output = bit.input[0] and bit.input[0]
            return bit.output

    class NotGate(Gate):
        """ class representing NOT gate """

        def logic(bit):
            bit.output = not bit.input[0]
            return bit.output

    class NandGate(AndGate,NotGate):

        def logic(bit):
            bit.flag = AndGate.logic(bit)
            Gate.__init__(bit,bit.flag)
            bit.output = NotGate.logic(bit)
            return bit.output

    n = NandGate(a,b).logic()
    print(int(n))


while next_output :
    start()
在运行时,行中发生错误 n=与非门(a,b).logic()

  • 将成员函数中的第一个参数称为self是一种很强的约定,因为该参数指的是被调用的对象,而不是其他对象。就是

    def logic(self):
    
    说明它正在自己工作

  • 与非门应该有一个,而不是IsA和/或NotGate

    class NandGate(Gate):
      def logic(self):
        anded = AndGate(self.input).logic() # calculate the and'd values
        notted = NotGate(anded).logic() # not them
        return notted
    
    这不仅比让一个对象两者兼而有之更清晰,而且代码的重用更整洁,调用特定的uu init_u_u时跳过的麻烦更少

  • 根据您所展示的,门不完全需要有.output字段。它可以根据其.logic()计算结果并返回,只存储其输入

    def output(bit):
      """ the output of the gate """
      return bit.logic()
    
  • AndGate的逻辑被破坏——它检查[0]两次。它应该检查self.input[0]和self.input[1]

  • 很难说tabing是否被堆栈溢出破坏了,但不清楚Gates的init、逻辑和输出函数放在哪里了

  • 将活动逻辑置于两个定义之间肯定是不好的:

    class Gate():
      def __init__():
        # definitions
    
    class NotGate(Gate): 
      # more definitions
    
    # functional flow of program
    a = int(input("Enter the first input: "))
    b = int(input("Enter the second input: "))
    n = NandGate(a,b).logic()
    print(int(n))
    
  • 对于第5点和第6点,完全有可能错误只是来自一个被声明为Gate的一部分,而不是全局变量作为运行时程序的一部分

  • 将成员函数中的第一个参数称为self是一种很强的约定,因为该参数指的是被调用的对象,而不是其他对象。就是

    def logic(self):
    
    说明它正在自己工作

  • 与非门应该有一个,而不是IsA和/或NotGate

    class NandGate(Gate):
      def logic(self):
        anded = AndGate(self.input).logic() # calculate the and'd values
        notted = NotGate(anded).logic() # not them
        return notted
    
    这不仅比让一个对象两者兼而有之更清晰,而且代码的重用更整洁,调用特定的uu init_u_u时跳过的麻烦更少

  • 根据您所展示的,门不完全需要有.output字段。它可以根据其.logic()计算结果并返回,只存储其输入

    def output(bit):
      """ the output of the gate """
      return bit.logic()
    
  • AndGate的逻辑被破坏——它检查[0]两次。它应该检查self.input[0]和self.input[1]

  • 很难说tabing是否被堆栈溢出破坏了,但不清楚Gates的init、逻辑和输出函数放在哪里了

  • 将活动逻辑置于两个定义之间肯定是不好的:

    class Gate():
      def __init__():
        # definitions
    
    class NotGate(Gate): 
      # more definitions
    
    # functional flow of program
    a = int(input("Enter the first input: "))
    b = int(input("Enter the second input: "))
    n = NandGate(a,b).logic()
    print(int(n))
    

  • 对于第5点和第6点,完全有可能错误只是来自一个被声明为Gate的一部分,而不是作为运行时程序的一部分的全局变量。

    您输入的代码在我的计算机上运行得非常好。如果不直接使用任何一个类的方法,为什么要从
    AndGate
    NotGate
    继承
    NandGate
    ?只需从
    门继承
    。但用于定义操作。应该提到所有需要的门。获得的输出是回溯(最后一次调用):文件“C:\Users\Rhea M Pradeep\Documents\python\trail.py”,第54行,在start()文件“C:\Users\Rhea M Pradeep\Documents\python\trail.py”,第49行,在start n=NandGate中(Gate).logic()类型错误:object()不带任何参数您发布的代码中没有这行代码…您放置的代码在我的计算机上运行良好。如果您不直接使用任何一个类的方法,为什么要从
    继承
    NandGate
    NotGate
    ?只需从
    Gate继承即可。但是为了定义一个操作,所有的门都是必需的应该提到的是,获得的输出是回溯(最后一次调用):文件“C:\Users\Rhea M Pradeep\Documents\python\trail.py”,第54行,在start()文件“C:\Users\Rhea M Pradeep\Documents\python\trail.py”,第49行,在start n=NandGate(Gate).logic()中TypeError:object()不接受您发布的代码中没有的行的参数…问题只在循环时出现问题只在循环时出现问题