Sorry, meant to respond to the list, not just the OP...<br><br><div class="gmail_quote">---------- Forwarded message ----------<br>From: <b class="gmail_sendername">Marc Tompkins</b> &lt;<a href="mailto:marc.tompkins@gmail.com">
marc.tompkins@gmail.com</a>&gt;<br>Date: Dec 28, 2007 12:13 AM<br>Subject: Re: [Tutor] Dynamically named objects<br>To: Michael Bernhard Arp Sørensen &lt;<a href="mailto:michaelarpsorensen@stevnstrup.dk">michaelarpsorensen@stevnstrup.dk
</a>&gt;<br><br><br>I don&#39;t think there&#39;s any need for eval - I&#39;ll leave the sermon about why eval is a Bad Thing for someone else - and if your objects are going to spend most of their &quot;lives&quot; in a list or dictionary, they don&#39;t really need meaningful individual names.&nbsp; (I mean, instead of naming each one class1, class2, etc. you can simply refer to them as classDict[1], classDict[2], etc. or classList[1], etc.)
<br><br>How&#39;s this:<br>#====================================<br>class thingy():<br>&nbsp;&nbsp;&nbsp; example = &quot;Just testing - &quot;<br>&nbsp;&nbsp;&nbsp; def __init__(self, num):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.example = self.example + str(num)<br><br>thang = {}
<br>for x in range(1,4):<br>&nbsp;&nbsp;&nbsp; thang[x] = thingy(x)<br><br>for item, value in thang.iteritems():<br>&nbsp;&nbsp;&nbsp; print item, value.example<br>#=====================================<br><br><div class="gmail_quote"><div><div></div>
<div class="Wj3C7c">On Dec 27, 2007 11:44 PM, Michael Bernhard Arp Sørensen &lt;
<a href="mailto:michaelarpsorensen@stevnstrup.dk" target="_blank">michaelarpsorensen@stevnstrup.dk</a>&gt; wrote:<br></div></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div><div></div><div class="Wj3C7c">
Hi there.<br><br>I need to instantiate objects on the fly and put the in a list/dict for later use. I was thinking of this:<br><br>objectlist = []<br>newobjectname = &quot;object1&quot;<br>classname = &quot;class1&quot; + &quot;()&quot;
<br>objectlist.append(newobjectname = eval(classname) )<br>objectlist[0].method(&quot;hello world&quot;)<br clear="all"><br>Can this be done? If not, is there a work around or some design pattern for this situation?<br><br>


-- <br>Venlig hilsen/Kind regards<br><br>Michael B. Arp Sørensen<br>Programmør / BOFH<br><br>I am /root and if you see me laughing you better have a backup.
<br></div></div>_______________________________________________<br>Tutor maillist &nbsp;- &nbsp;<a href="mailto:Tutor@python.org" target="_blank">Tutor@python.org</a><br><a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">
http://mail.python.org/mailman/listinfo/tutor
</a><br><br></blockquote></div><br><br clear="all"><br>-- <br><a href="http://www.fsrtechnologies.com" target="_blank">www.fsrtechnologies.com</a>
</div><br><br clear="all"><br>-- <br><a href="http://www.fsrtechnologies.com">www.fsrtechnologies.com</a>