Python Forum
Init an indefinite number of class - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Init an indefinite number of class (/thread-36424.html)



Init an indefinite number of class - MathisDELAGE - Feb-18-2022

Hello world,
I need your help for my new game project.
I want to use x(the number of player) time my class Player.
I think do that but it's not optimum.

from MyClass import Player
x = int(input("Number of player: "))

for i in range(1, x+1):
    exec(f"Player{i} = Player()")
Except that I need to do this for each round of the game.
Can you recommend something better?
Thank you in advance.


RE: Init an indefinite number of class - ndc85430 - Feb-18-2022

You shouldn't be using exec, you should be storing the items in a collection (e.g. tuple, list, set, or dict as appropriate).


RE: Init an indefinite number of class - MathisDELAGE - Feb-18-2022

(Feb-18-2022, 01:35 PM)ndc85430 Wrote: You shouldn't be using exec, you should be storing the items in a collection (e.g. tuple, list, set, or dict as appropriate).

I see thank you


RE: Init an indefinite number of class - dboxall123 - Feb-18-2022

I don't understand what you're doing on that bottom line, couldn't you just do this?:
players = []
for i in range(1, x+1):
    players.append(Player())
Or maybe even:
players = [Player()] * x



RE: Init an indefinite number of class - ndc85430 - Feb-18-2022

Do you not know about collections of things at all? How are you learning Python? Collections should be covered in whatever introductory book, course, etc. you're using, as they're pretty fundamental.


RE: Init an indefinite number of class - MathisDELAGE - Feb-18-2022

(Feb-18-2022, 01:40 PM)dboxall123 Wrote: I don't understand what you're doing on that bottom line, couldn't you just do this?:
players = []
for i in range(1, x+1):
    players.append(Player())

Thanks it's exatly what I want to do
I don't know why I'm not think to that before


RE: Init an indefinite number of class - ndc85430 - Feb-18-2022

Really, that's nearer as a list comprehension: players = [Player() for _ in range(1, x + 1)]


RE: Init an indefinite number of class - dboxall123 - Feb-18-2022

(Feb-18-2022, 01:48 PM)ndc85430 Wrote: Really, that's nearer as a list comprehension: players = [Player() for _ in range(1, x + 1)]
Wouldn't this be easier players = [Player()] * x


RE: Init an indefinite number of class - MathisDELAGE - Feb-18-2022

(Feb-18-2022, 01:40 PM)dboxall123 Wrote: I don't understand what you're doing on that bottom line, couldn't you just do this?:
players = []
for i in range(1, x+1):
    players.append(Player())
Or maybe even:
players = [Player()] * x

I know but I'm don't know why I don't thing to that before


RE: Init an indefinite number of class - deanhystad - Feb-18-2022

Quote:Wouldn't this be easier players = [Player()] * x
Nope. If you do this you end up with 1 player. players[0] would be the same player as players[1] and so on. This is a common problem where programmers try to create a list of mutable objects using the "*" operator.

Nobody complains if "numbers = [0] * count" returns a list where numbers[0] is numbers[1], but it is usually not the desired result when trying to make a list of lists. "lists = [[]] * count" works exactly the same as "numbers", where lists[0] is lists[1]. In both cases you have a list holding a single value that is used to create a list holding multiple of the value. if you want a different value for each item in the list you need a different generator.

To make a list containing different numbers you might do this
numbers = [number for number in range(count)]
To build a list of different lists you do this:
lists = [[] for _ in range(count)] 
And to build a list of different players you do this:
players = [Player() for _ in count]