I am trying to learn about OOP and inheritance concepts as well as defining a function with an asterisk *
before a variable (to unpack its values). When executing the toy code below, I got either an unexpected keyword argument or invalid syntax error.
class A:
def __init__(self, *position):
self.max_hp = 100
self.current_hp = 100
self.position = position
class B(A):
def __init__(self, max_hp=80, *position):
super().__init__(*position)
self.max_hp = max_hp
self.current_hp = max_hp
test = B(position = (20, 30))
print(test, test.max_hp, test.current_hp, test.position)
TypeError: B.__init__() got an unexpected keyword argument 'position'
test = B(*position = (20, 30))
print(test, test.max_hp, test.current_hp, test.position)
test = B(*position = (20, 30))
^
SyntaxError: invalid syntax
Why didn’t the function recognise the argument position
? How can I fix it?