"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?
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
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?
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