Skip to content Skip to sidebar Skip to footer

__init__() Got Unexpected Keyword Argument 'y'

I'm following the book, 'Python Programming For The Absolute Beginner' and decided to test some of my skills by making my own game. The game is basically 'don't get hit by the flyi

Solution 1:

This line:

the_pig = Player(image = pig_image,
                 x = games.mouse.x,
                 y = games.mouse.y)

Is (sort of) equivalent to:

the_pig = Player.__init__(image = pig_image,
                 x = games.mouse.x,
                 y = games.mouse.y)

This means your __init__ should accept the arguments image, x and y, but you have defined it as:

def__init__(self):
    """A test!"""print("Test.")

If you want to simple pass on all the arguments, you can do it like this:

def__init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)
    """A test!"""print("Test.")

This uses the * and ** syntax to get all the arguments and keyword arguments and then uses super to call the super-classes __init__ with those arguments.

The alternative (more work) is:

def__init__(self, image, x, y):
    super().__init__(image=image, x=x, y=y)
    """A test!"""print("Test.")

Post a Comment for "__init__() Got Unexpected Keyword Argument 'y'"