Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

What does Game() do?

I noticed that when I ran the script before the last line with "Game()", nothing happened. Could you explain what Game() did?

1 Answer

Steven Parker
Steven Parker
243,215 Points

Since you didn't link to a course or show any code, this is a bit of a guess.

But I suspect that prior to the last line, your code only defines functions, and the main program function is named Game. Definitions don't make anything happen, they just get things ready. It's like handing the computer a list of instructions but saying "don't start yet".

But on the last line, Game() is invoking the function, which causes the previous definition to actually run. It's like saying "start following those instructions now".

Oh. I'm sorry. The video was at the end of Object-Oriented Python, where we finished the game. So Game is a class, not a function.

Steven Parker
Steven Parker
243,215 Points

It's still the same idea.

Defining a class doesn't make anything happen either. But placing parentheses after the class name causes an instance of the class to be constructed. This involves invoking the __init__ method of the class, which in this case contains the main program loop.

Oh. I see. So if I typed Game() the instance would be called "Game", right?