"TypeError: Person() takes 1 positional argument but 2 were given" but the constructor takes two arguments

Question:

This is the strangest thing. My python has just stopped handling classes with multiple parameters in their ctors? Running python 3.8.10 getting error TypeError: Person() takes 1 positional argument but 2 were given

def Person(object):
    def __init__(self, a, b):
        self.aa = a
        self.bb = b

pp = Person(20, 40)

If I bring the Person __init__ down to one parameter, then it works. If I raise it to 3, then I get the same takes 1 but 3 were given error. I’m totally stumped?

Asked By: user2183336

||

Answers:

You’ve declared your Person class wrong. You used def instead of class, meaning that you actually have a function called Person with a local function called __init__.

Try this:

class Person(object):
    def __init__(self, a, b):
        self.aa = a
        self.bb = b
Answered By: Rafael de Bem
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.