Python Forum
How to create a varying qty of instances of a Class and name them?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to create a varying qty of instances of a Class and name them?
#1
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?
Reply
#2
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.
Reply
#3
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [solved] Classes, assign an attributes to a class not to instances.. SpongeB0B 4 891 May-20-2023, 04:08 PM
Last Post: SpongeB0B
  Cannot convert the series to <class 'int'> when trying to create new dataframe column Mark17 3 8,390 Jan-20-2022, 05:15 PM
Last Post: deanhystad
  Accessing varying command line arguements Rakshan 3 2,009 Jul-28-2021, 03:18 PM
Last Post: snippsat
  Create Generator in the Class quest 1 2,090 Apr-15-2021, 03:30 PM
Last Post: jefsummers
  How to create and define in one line a 2D list of class objects in Python T2ioTD 1 1,989 Aug-14-2020, 12:37 PM
Last Post: Yoriz
  Class Instances called in the wrong order IanIous 4 2,777 Mar-06-2020, 02:16 PM
Last Post: IanIous
  How to access class variable? instances vs class drSlump 5 3,286 Dec-11-2019, 06:26 PM
Last Post: Gribouillis
  Have two class instances affect one another The_Zookinator 3 2,250 Jun-26-2019, 06:56 PM
Last Post: The_Zookinator
  How I can create reference to member of the class instance? AlekseyPython 8 3,752 Dec-05-2018, 06:24 AM
Last Post: AlekseyPython
  Class Instances overriding class members and attributes. Leaf 7 6,863 Nov-29-2017, 06:08 AM
Last Post: Leaf

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020