<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Mar 9, 2015 at 12:54 PM, Serhiy Storchaka <span dir="ltr"><<a href="mailto:storchaka@gmail.com" target="_blank">storchaka@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On 09.03.15 17:48, Neil Girdhar wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
So you agree that the ideal solution is composition, but you prefer<br>
inheritance in order to not break code?<br>
</blockquote>
<br></span>
Yes, I agree. There is two advantages in the inheritance: larger backward compatibility and simpler implementation.<span class=""><br>
<br></span></blockquote><div><br></div><div>Inheritance might be more backwards compatible, but I believe that you should check how much code is genuine not restricted to the idealized flags interface.   It's not worth talking about "simpler implementation" since the two solutions differ by only a couple dozen lines.</div><div><br></div><div>On the other hand, composition is better design.  It prevents you from making mistakes like adding to flags and having carries, or using flags in an unintended way.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Then,I think the big question<br>
is how much code would actually break if you presented the ideal<br>
interface.  I imagine that 99% of the code using flags only uses __or__<br>
to compose and __and__, __invert__ to erase flags.<br>
</blockquote>
<br></span>
I don't know and don't want to guess. Let just follow the way of bool and IntEnum. When users will be encouraged to use IntEnum and IntFlags instead of plain ints we could consider the idea of dropping inheritance of bool, IntEnum and IntFlags from int. This is not near future.</blockquote><div><br></div><div>I think it's the other way around.  You should typically start with the modest interface and add methods as you need.  If you start with full blown inheritance, you will find it only increasingly more difficult to remove methods in changing your solution.  Using inheritance instead of composition is one of the most common errors in objected oriented programming, and I get the impression from your other paragraph that you're seduced by the slightly shorter code.  I don't think it's worth giving in to that without proof that composition will actually break a significant amount of code.  </div><div><br></div><div>Regarding IntEnum — that should inherit from int since they are truly just integer constants.  It's too late for bool; that ship has sailed unfortunately.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=""><br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
    > Here's another reason.  What if someone wants to use an IntFlags object,<br>
    > but wants to use a fixed width type for storage, say numpy.int32?   Why<br>
    > shouldn't they be able to do that?  By using composition, you can easily<br>
    > provide such an option.<br>
    You can design abstract interface Flags that can be combined with<br>
    int or other type. But why you want to use numpy.int32 as storage?<br>
    This doesn't save much memory, because with composition the IntFlags<br>
    class weighs more than int subclass.<br>
Maybe you're storing a bunch of flags in a numpy array having dtype<br>
np.int32?  It's contrived, I agree.<br>
</blockquote>
<br></span>
I afraid that composition will not help you with this. Can numpy array pack int-like objects into fixed-width integer array and then restore original type on unboxing?</blockquote><div><br></div><div>You're right. </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5"><br>
<br>
<br>
______________________________<u></u>_________________<br>
Python-Dev mailing list<br>
<a href="mailto:Python-Dev@python.org" target="_blank">Python-Dev@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-dev" target="_blank">https://mail.python.org/<u></u>mailman/listinfo/python-dev</a><br>
Unsubscribe: <a href="https://mail.python.org/mailman/options/python-dev/mistersheik%40gmail.com" target="_blank">https://mail.python.org/<u></u>mailman/options/python-dev/<u></u>mistersheik%40gmail.com</a><br>
</div></div></blockquote></div><br></div></div>