<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div>On Mar 8, 2015, at 1:45 AM, Neil Girdhar <<a href="mailto:mistersheik@gmail.com">mistersheik@gmail.com</a>> wrote:</div><div><br></div><blockquote type="cite"><div><div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Mar 7, 2015 at 3:38 PM, Ethan Furman <span dir="ltr"><<a href="mailto:ethan@stoneleaf.us" target="_blank">ethan@stoneleaf.us</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div>On 03/07/2015 12:28 PM, Georg Brandl wrote:<br>
> On 03/07/2015 04:05 PM, Ethan Furman wrote:<br>
><br>
>>>> As long as we are dreaming :)<br>
>>>><br>
>>>> class Stat(IntFlag):<br>
>>>> RDONLY = 1<br>
>>>> NOSUID = 2<br>
>>>> NODEV = 4<br>
>>>> NOEXEC = 8<br>
>>>> SYNCHRONOUS = 16<br>
>>>> MANDLOCK = 64<br>
>>>> WRITE = 128<br>
>>>> APPEND = 256<br>
>>>> NOATIME = 1024<br>
>>>> NODIRATIME = 2048<br>
>>>> RELATIME = 4096<br>
>>>><br>
>>>> a = Stat.RDONLY # creates a new instance of Stat, not a singleton<br>
>>><br>
>>> Why?<br>
>><br>
>> Because by having mutable instances of Stat we can have more Python operations:<br>
>><br>
>> instead of:<br>
>><br>
>> x = Stat(some_Stat_value_from_somewhere)<br>
>> x = x | Stat.NOEXEC # to set the bit<br>
>><br>
>> we can say:<br>
>><br>
>> x = Stat(some_Stat_value_from_somewhere)<br>
>> x.NOEXEC = True<br>
><br>
> Please no. You're making a mutable type out of something that is conceptually<br>
> (and in people's minds) an integer. Remember how long it can take to understand<br>
> that<br>
><br>
> a = 1<br>
> a = 2<br>
><br>
> does not change the integer "1" to now be "2".<br></div></div></blockquote><div><br></div><div>The fact that the flags are stored in an int — that is, the implementation has nothing at all to do with the conceptual nature of flags. Flags are properties. Your knowledge of the underlying implementation is misleading you. It would be just as easy and intuitive to implement those flags as keys and values in a dict.</div></div></div></div></div></blockquote><div><br></div><div>Usually that's true. And when it's true, you use a dict (or a set or a namedtuple or whatever's appropriate), so you have no need for IntFlags in the first place. The only reason you'd ever want this class is when the fact that the flags are stored in an int is important.</div><div><br></div><div>Sure, an IntFlags that isn't a subclass on int can define an __int__ method. But so can some class that doesn't use an int for storage in the first place. If you want to convert back and forth, that's perfectly fine, but why are you using an int for storage?</div><br><blockquote type="cite"><div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div>If flags were conceptually subtypes of int, then you should be able to do things like:</div><div><br></div><div>flags ** 7</div><div><br></div><div>or</div><div><br></div><div>flags // 91</div><div><br></div><div>Do you agree that this is totally meaningless? There is no "is a" relationship between Flags and int. There is a conversion between the conceptual mapping that is a Flags object to int for the sole purpose of calling into APIs.</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div>
<br>
</div></div>Good point. To do something like that the name would have be BitFlags or something no so solidly tied to "immutable".<br>
At any rate, for this to work would require `int(x)` around every call to a lower-level API, and that's a non-starter.<br></blockquote><div><br></div><div>Correct me if I'm wrong, but doesn't Boost.Python when generating Python methods that accept ints, automatically call __int__ on the arguments. Doesn't SWIG do the same? Can you give me some examples of methods that accept ints but don't call __int__? It seems to me to be a bug in those methods than in the caller. A method that wants an int should call __int__ on its argument — not expect that isinstance(X, int) etc.</div><div> <br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
--<br>
~Ethan~<br>
<div><div><br></div></div></blockquote></div></div></div>
</div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>Python-ideas mailing list</span><br><span><a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a></span><br><span><a href="https://mail.python.org/mailman/listinfo/python-ideas">https://mail.python.org/mailman/listinfo/python-ideas</a></span><br><span>Code of Conduct: <a href="http://python.org/psf/codeofconduct/">http://python.org/psf/codeofconduct/</a></span></div></blockquote></body></html>