Dynamically naming objects.

Paul reverselogic at gmail.com
Fri Jun 6 23:39:34 EDT 2008


Something like this?

class User:
        def __init__(self, name):
                self.name = name
        def __str__(self):
                return self.name
n = 10
users = []

for i in range(n):
        users.append(User('user%d' % i))

print users[9]
print users[4]

Cheers,

Paul


On Sat, Jun 7, 2008 at 3:59 AM, Kalibr <space.captain.face at gmail.com> wrote:

> I've been developing a small script to fiddle with classes, and came
> accross the following problem. Assuming I get some user input asking
> for a number, how would I spawn 'n' objects from a class?
>
> i.e. I have a class class 'user' and I don't know how many of them I
> want to spawn.
>
> Any ideas?
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080607/d98bf272/attachment-0001.html>


More information about the Python-list mailing list