I have the following code:
class Player():
"""Initialization examples:
player = Player('Lebron', 'James')
player = Player.by_id(2544)
"""
def __init__(self, first_name, last_name):
"""Search for a given player matching first_name and last_name.
The matching is case insensitive.
"""
self.player = self._get_player_by_name(
first_name.lower(), last_name.lower())
@classmethod
def by_id(self, player_id):
"""Search for a given player by thier nba.com player id.
Args:
player_id: str representation of an nba.com player id
e.g. Lebron's player id is 2544 and his nba.com link is
https://stats.nba.com/player/2544/
Intended as an alternate Player constructor.
"""
self.player = self._get_player_by_id(str(player_id))
def _get_player_by_id(self, player_id):
pass
However, when calling Player.by_id(2544), I get the following error:
TypeError: _get_player_by_id() missing 1 required positional argument: 'player_id'
What's going on here? Most questions I've searched just involve adding the self argument, which I already have.
by_idis not an instance ofPlayer, so don't call itself. It'sPlayeritself (or a subclass thereof), and should be namedclsto indicate it's a class, not an instance. Trying to call instance methods on classes doesn't work unless you pass an instance as the first argument, and you didn't.