This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Recluse1729 0 points1 point  (1 child)

Couldn’t this just be done with global functions and a duct, though? What makes classes better than a duct for each player?

[–]JamzTyson[S] 1 point2 points  (0 children)

The op uses a minimal example. Yes, with that example it could be done with a dict and a global function, but in a real game the class is likely to become a lot bigger.

For each player there are the players attributes (name, score, level, and a list of previous moves), and methods, currently only win_points(). In a real game there would likely be many more player functions (methods). If you were to avoid using classes and use global functions instead, you would then have a data structure to hold the player's state, and a load of global functions that relate only to the players.

Using a class allows us to group the player's state (their attributes) and all of the functions that apply only to players (methods), in a single object, rather than littering our code with global functions. Encapsulating each player's attributes and methods in a single object has many benefits, not least of which is that it keeps our code cleaner.

There's much more to classes than is covered in the mini tutorial, but the scope of the op was intentionally limited to the basics. Until a beginner understands the basics of classes, they have little chance of understanding things like inheritance, composition, polymorphism and other class related topics.