Of course that would be:<div><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0,8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; ">
foo = lambda condition: MyClass() if condition else None</blockquote><br><div class="gmail_quote">2011/3/3 Izz ad-Din Ruhulessin <span dir="ltr">&lt;<a href="mailto:izzaddin.ruhulessin@gmail.com">izzaddin.ruhulessin@gmail.com</a>&gt;</span><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Maybe:<div><br></div><div><blockquote class="gmail_quote" style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0,8ex;border-left-width:1px;border-left-color:rgb(204, 204, 204);border-left-style:solid;padding-left:1ex">

foo = lambda x: MyClass() if condition else None</blockquote><br><div class="gmail_quote">2011/3/3 David <span dir="ltr">&lt;<a href="mailto:bouncingcats@gmail.com" target="_blank">bouncingcats@gmail.com</a>&gt;</span><div>
<div></div><div class="h5"><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Another classic case of trying something not the best way, due to<br>
 inexperience.<br>
But it has been a good process: I learned something new from<br>
setting myself the initial puzzle and then finding a solution,and then<br>
learned more from the great tutoring here. Thanks very much for all<br>
the replies.<br>
<div><br>
On 2 March 2011 03:31, Alan Gauld &lt;<a href="mailto:alan.gauld@btinternet.com" target="_blank">alan.gauld@btinternet.com</a>&gt; wrote:<br>
&gt;<br>
&gt;&gt; class MyClass_2(object):<br>
&gt;&gt;    def __new__(self, condition):<br>
&gt;&gt;         if condition:<br>
&gt;&gt;               return object.__new__(self)<br>
&gt;&gt;         else:<br>
&gt;&gt;               return None<br>
&gt;<br>
</div><div>&gt; Thats pretty much how I&#39;d do it.<br>
<br>
</div>Thanks for reviewing my code.<br>
<div><br>
On 2 March 2011 03:35, Alan Gauld &lt;<a href="mailto:alan.gauld@btinternet.com" target="_blank">alan.gauld@btinternet.com</a>&gt; wrote:<br>
&gt;<br>
&gt; Oops, sent too soon.<br>
&gt;<br>
&gt; I meant to add that you should realize that the implication of your<br>
&gt; design is that the user of the class now has to check each object<br>
&gt; to see if it is a valid reference or None. You could raise an exception<br>
&gt; instead of returning None which allows a try/except style...<br>
&gt;<br>
&gt; This extra overhead is one reason these kinds of &quot;clever&quot; tricks<br>
&gt; are usually avoided. A valid object with null content is often<br>
&gt; preferrable, or a singleton style pattern. But occasionally your<br>
&gt; style is needed, just be aware of the extra overhead you<br>
&gt; introduce by using it.<br>
<br>
</div>Spot on. It would require two &quot;if&quot; tests, one inside __new__() and<br>
another in the code.<br>
<br>
I found your mention of try/except there especially helpful, because<br>
it was a pertinent reminder that I was not thinking in &quot;ask forgiveness<br>
not permission&quot; mode. This (newbie mistake) occurred because I<br>
wanted my application to continue, not abort with an exception, but<br>
after your prompt I recalled that &quot;except&quot; doesn&#39;t have to raise exceptions<br>
it can do other things.<br>
<br>
So I went in the direction you suggested and I am happy with the results.<br>
Basically my application is interpreting binary file data by instantiating a<br>
structured object for each file in a fixed list of binary files, and I<br>
was looking<br>
for a neat way to ignore any errors on files that might not be present<br>
(failed to open). So, after feedback here my solution now is to use<br>
try/except in the class __init__() to create a valid object with null content,<br>
and then use &quot;if&quot; tests in the rest of the code that processes the objects to<br>
just ignore them if they are null, which is a nice clear way to do it.<br>
<div><br>
On 2 March 2011 20:44, Steven D&#39;Aprano &lt;<a href="mailto:steve@pearwood.info" target="_blank">steve@pearwood.info</a>&gt; wrote:<br>
&gt;<br>
&gt; By convention, the name of the first argument to __new__ is cls, not self,<br>
&gt; because it is bound to the class object itself (MyClass_2 in this example)<br>
&gt; rather than the instance. The instance doesn&#39;t yet exist, so that&#39;s not<br>
&gt; surprising!<br>
<br>
</div>Thanks for pointing that out. In 2.6 Language Reference 3.4.3 they used<br>
&quot;mcs&quot; (metaclass?) which I didn&#39;t comprehend at all at the time (the mindset<br>
of just wanting to get some code working to fix a problem is not the most<br>
helpful mindset for decoding a large body of new information), so I just used<br>
&quot;self&quot; when getting their example code to work for me. In Section 3.4.1 they<br>
use &quot;cls&quot; which I now see clearly and understand thanks.<br>
<div><br>
On 3 March 2011 03:03, Knacktus &lt;<a href="mailto:knacktus@googlemail.com" target="_blank">knacktus@googlemail.com</a>&gt; wrote:<br>
&gt;<br>
&gt; I think that&#39;s too clever ;-).<br>
<br>
</div>I agree now .. but it was a useful experiment. Thanks for the tute.<br>
<div><div></div><div>_______________________________________________<br>
Tutor maillist  -  <a href="mailto:Tutor@python.org" target="_blank">Tutor@python.org</a><br>
To unsubscribe or change subscription options:<br>
<a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br>
</div></div></blockquote></div></div></div><br></div>
</blockquote></div><br></div>