Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Testing Be Assertive Membership and Other Assertions

Anthony Grodowski
Anthony Grodowski
4,902 Points


Could someone please explain me what .grup() function does? I've seen it in the dice.py code but have no idea what it does since I think it hasn't been covered in any of the Python course so far. Thanks!

1 Answer

Hopefully, I understand your question right. I think you are referring to this line of code in dice.py

num, sides = die_pattern.match(description).groups()

This is associated with Python Regular Expression. I am providing this link here for you. Remember how Kenneth Love mentions that you can make a roll of dice with a string, e.g. '1d6'. If this description string you provide matches with the die_pattern

die_pattern = re.compile(r'^(?P<num>\d+)d(?P<sides>\d+)$', re.I)

then the function groups() will return a tuple containing all the subgroups which mean the matched text. That is why you would see that these two matched texts are being assigned to num and sides

num, sides = die_pattern.match(description).groups()

Hope that this helps you!