Welcome to the Treehouse Community
Looking to learn something new?
using groupdict() method to pass re.search patterns into a class
Wow! OK, now, create a class named Player that has those same three attributes, last_name, first_name, and score. I should be able to set them through init.
So I'm using the groupdict() on my players (list? tuple? I'm actually not sure what re.search gives us) . Not sure what is exactly wrong.
import re string = '''Love, Kenneth: 20 Chalkley, Andrew: 25 McFarland, Dave: 10 Kesten, Joy: 22 Stewart Pinchback, Pinckney Benton: 18''' players = re.search(r''' (?P<last_name>[\w\s]+),\s (?P<first_name>[\w\s]+):\s (?P<score>[\d]+) ''', string, re.X| re.M) class Player: def __int__(self, **players.groupdict()): self.last_name = players.groupdict('last_name') self.first_name = players.groupdict('first_name') self.score = players.groupdict('score')
Dan Johnson40,532 Points
search returns a match object, which is why it has the groupdict method.
For your class definition there's a few things. For the magic method:
def __int__(self, **players.groupdict()):
You were probably looking for
__init__ instead of
__int__ also happens to be a magic method, but it's for how you deal with conversions to an int.
Another thing is that since you're defining the function you can't put
**players.groupdict() in the signature, you just want to define a name for that argument. You will, however, want to define the
__init__ method in such a way that you could unpack the result of groupdict in order to construct a
Player, like this:
# ... class Player(object): def __init__(self, last_name, first_name, score): # Do all the assignment here. No call to groupdict # is required. # You don't need this line for the challenge. # This is just to show how you could create # a new player with groupdict. example = Player(**players.groupdict())