0

I'm a beginner and while trying for a program error: TypeError: object() takes no parameters is frequently occurring.

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()

while running error occurs in line n = NandGate(a,b).logic()

4
  • The code you put works perfectly on my computer. And why inherit NandGate from AndGate and NotGate if you don't use methods from either class directly? Just inherit from Gate. Commented Dec 29, 2017 at 19:37
  • But for defining an operation. Which all gates is needed should be mentioned Commented Dec 30, 2017 at 2:54
  • The output obtained is Traceback (most recent call last): File "C:\Users\Rhea M Pradeep\Documents\python\trail.py", line 54, in <module> start() File "C:\Users\Rhea M Pradeep\Documents\python\trail.py", line 49, in start n = NandGate(Gate).logic() TypeError: object() takes no parameters Commented Dec 30, 2017 at 3:59
  • That line isn't in the code that you posted... Commented Dec 30, 2017 at 13:56

1 Answer 1

2
  1. It's a strong convention to have the first parameter in member functions be called self, as that one refers to the object it's being called on, and not some other object. That is,

    def logic(self):
    

    clarifies that it's working on itSELF.

  2. A NandGate should probably HaveA, rather than IsA And/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
    

    Not only is this clearer than having an object be both, it's tidier reuse of code and less hoops to jump through with specific __init__s being called.

  3. Based on what you've shown, it's not entirely necessary for a gate to have a .output field. It could just calculate the result from its .logic() and return that, storing only its input.

    def output(bit):
      """ the output of the gate """
      return bit.logic()
    
  4. The AndGate's logic is busted--it checks the [0] twice. It should check self.input[0] and self.input[1].

  5. It's hard to tell if the tabbing just got butchered by Stack Overflow or not, but it's unclear where Gates's init, logic, and output functions got placed.

  6. It's definitely bad form to put active logic in between definitions:

    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))
    

It's entirely possible that with point 5 and 6, the error is just coming from a being declared as a part of Gate, and NOT global variables as part of your runtime program.

Sign up to request clarification or add additional context in comments.

1 Comment

The problem is arising only when it is looped

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.