Python Forum

Full Version: How to create a varying qty of instances of a Class and name them?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
For a very simple dice game I intend to create a class with attributes of name and score. It is straight forward to create this and also to create objects of player1, player2 and so on. However if I don't know how many player objects I need as the number of players is dynamic, can I make a function which will create and name these player objects?
Quote:However if I don't know how many player objects I need as the number of players is dynamic, can I make a function which will create and name these player objects?
You can use a loop to create the needed quantity of player instances, these can be store in a list or a dictionary whichever is most suitable.
Thank you for the quick reply. I will look into the way you suggest. Being new to learning classes and objects I will look for code examples. What you have said does make sense.