<div dir="ltr">On 5 April 2013 02:16, Ethan Furman <span dir="ltr"><<a href="mailto:ethan@stoneleaf.us" target="_blank">ethan@stoneleaf.us</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im">On 04/04/2013 08:01 AM, Chris Angelico wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On Fri, Apr 5, 2013 at 1:59 AM, Guido van Rossum <<a href="mailto:guido@python.org" target="_blank">guido@python.org</a>> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On Thu, Apr 4, 2013 at 7:47 AM, Chris Angelico <<a href="mailto:rosuav@gmail.com" target="_blank">rosuav@gmail.com</a>> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Is there any argument that I can pass to Foo() to get back a Bar()?<br>
Would anyone expect there to be one? Sure, I could override __new__ to<br>
do stupid things, but in terms of logical expectations, I'd expect<br>
that Foo(x) will return a Foo object, not a Bar object. Why should int<br>
be any different? What have I missed here?<br>
</blockquote>
<br>
<br>
A class can define a __new__ method that returns a different object. E.g.<br>
(python 3):<br>
</blockquote>
<br>
Right, I'm aware it's possible. But who would expect it of a class?<br>
</blockquote>
<br></div>
FTR I'm in the int() should return an int camp, but to answer your question: my dbf module has a Table class, but it returns either a Db3Table, FpTable, VfpTable, or ClpTable depending on arguments (if creating a new one) or the type of the table in the existing dbf file.<br>
</blockquote><div><br></div><div style>I fall into:</div><div style><br></div><div style>1. int(), float(), str() etc should return that exact class (and operator.index() should return exactly an int).</div><div style><br>
</div><div style>2. It could sometimes be useful for __int__() and __index__() to return a subclass of int.</div><div style><br></div><div style>So, for the int constructor, I would have the following logic (assume appropriate try/catch):</div>
<div style><br></div><div style>def __new__(cls, obj):</div><div style>    i = obj.__int__()<br></div><div style><br></div><div style>    if type(i) is int:</div><div style>        return i</div><div style><br></div><div style>
    return i._internal_value<br></div><div style><br></div><div style>Tim Delaney</div></div></div></div>