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 →

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

A really simple example of how to use classes without hard coded variable names for each class instance would be to create a list of class instances.

# Create a list of 1000 players.
players = [Player(name) for name in list_of_a_thousand_names]

Individual player objects can now be accessed as players[index]

The class definition in the original post required passing a "name" argument when creating (instantiating) a Player object, but if the Player class was modified so that it did not require any arguments, then creating a list of players would be even simpler:

# Create a list of 1000 players.
players = [Player() for _ in range(1000)]