Ethan Furman added the comment: > I am quite aware about what's the intended use, but you can't just > assume people will know about it.
Fair enough. > In my view, you must do one of two things: > > 1) (at least in documentation, and preferably in the code by raising > Exceptions at class definition time) forbid the use of Flags whose values are > not either a) powers of two, or b) bitwise or of some already declared values The closest I would get to forbidding something would be to forbid non-integers as values -- and I'm not going to do that. In general, Python doesn't take such steps (one notable exception being sum refusing to work with strings because it was such a common trap). > -or- > > 2) Specify and implement a robust algorithm for finding the "cover of > bits" for a given numeric value. If the intended algorithm is really > "pick the largest member value smaller than given value, subtract and > repeat until 0 remains", then it should be said so in the > documentation, and preferably some reasons given for the usage of that > exact algorithm. ("it was easiest to implement" does not rank very high > on my list.) How about "it works for me"? Seriously, if every bit is named then the exact repr used is irrelevant. If you don't like the standard method then write your own __repr__ method, because that's what we're talking about -- not the actual value (which isn't going to be affected by the algorithm for finding the "cover of bits", but the display of the names). > In other words, MyFlags(7) should either be illegal, or I should be > able to interpret what it will be by reading the documentation. Leaving > it unspecified is not acceptable IMO. Thankfully, docs can still change during beta. > (In case it isn't clear: I'm for option 1. I _don't_ intend to write > MyFlags ever in Python. But if I happen to write it unintentionally > (e.g. if I forget to declare 2), I would like Python to tell me I'm > doing something wrong.) We could certainly add a decorator that double-checks that, just like we have the `unique` decorator to ensure no duplicates in an Enum. Any idea what to call it? complete? named? allbits? > (But if you really want option 2 for some reason, that's ok too. I'm > just saying I would like it specified, with a rationale if it's not > too much of a problem.) (1) isn't going to happen, except possibly as a decorator. If you would like to contribute code and/or docs for (2) and/or a decorator for (1) I'd be happy to review. ---------- _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue23591> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com