[Tutor] Dynamically named objects

Marc Tompkins marc.tompkins at gmail.com
Fri Dec 28 09:27:25 CET 2007


Here's the same thing with a list instead of a dictionary:
#===========================================
class thingy():
    example = "Just testing - "
    def __init__(self, num):
        self.example = self.example + str(num)

thang = []
for x in range(1,4):
    thang.append(thingy(x))

for item, value in enumerate(thang):
    print item, value.example
#============================================


On Dec 27, 2007 11:44 PM, Michael Bernhard Arp Sørensen <
michaelarpsorensen at stevnstrup.dk> wrote:

> Hi there.
>
> I need to instantiate objects on the fly and put the in a list/dict for
> later use. I was thinking of this:
>
> objectlist = []
> newobjectname = "object1"
> classname = "class1" + "()"
> objectlist.append(newobjectname = eval(classname) )
> objectlist[0].method("hello world")
>
> Can this be done? If not, is there a work around or some design pattern
> for this situation?
>
> --
> Venlig hilsen/Kind regards
>
> Michael B. Arp Sørensen
> Programmør / BOFH
>
> I am /root and if you see me laughing you better have a backup.
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
www.fsrtechnologies.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20071228/0e106a2e/attachment.htm 


More information about the Tutor mailing list