Reputation: 6793
What is the most pythonic way to construct and hold a group of N distinct class instances? The following seems fairly concise, but I dislike the need for the unused _
variable, and I'm not sure if the use of the list container [...]
is necessary.
things = [lib.Thing() for _ in range(NUM_THINGS_TO_CREATE)]
The only requirement for the container is that it is iterable (not necessarily in any particular order).
And in case you're curious as to what possible use this could be, it's to construct a bunch of threading.Thread
instances that are each making socket
calls, so their effect is outside the scope of the interpreter / virtual machine.
Upvotes: 2
Views: 53
Reputation: 32987
The way you wrote it is exactly the most pythonic way IMO. You need some kind of container, and a list is the most obvious choice. Any iteration will involve a variable so I don't think there's any syntax that won't involve one in some form; the for _ in ...
form is as simple as you can make it.
Upvotes: 2