[Python-ideas] IntFlags

Chris Barker chris.barker at noaa.gov
Thu Mar 5 18:11:29 CET 2015


On Thu, Mar 5, 2015 at 9:03 AM, Luciano Ramalho <luciano at ramalho.org> wrote:

> The problem is that a set is by definition unordered, but the position
> of the bits in BitFlags is crucial,


Is it? or is that an implementation detail?

What I'm getting at is that if you use an integer to store bits, then the
nth bit is, well, the nth bit in the value. But you could conceptually
think of it as the bit with the name, 'n', in which case order no longer
matters.

-Chris




> so the name BitSet may not give
> the right idea.
>
> On Thu, Mar 5, 2015 at 1:58 PM, Eugene Toder <eltoder at gmail.com> wrote:
> > Maybe even call it BitSet, and model the interface based on frozenset,
> > except that every element is also a set of one. The operations will be:
> > X | Y -- union
> > X & Y -- intersection
> > X ^ Y -- symmetric difference
> > X - Y -- difference
> > X in Y == (X & Y) == X
> > len(X) -- number of set bits
> > bool(X) -- any bits set
> > isdisjoint, issubset, issuperset == not X & Y, not X - Y, not Y - X
> > (Note no negation.)
> >
> > Eugene
> >
> > On Thu, Mar 5, 2015 at 11:15 AM, Luciano Ramalho <luciano at ramalho.org>
> > wrote:
> >>
> >> I like the IntFlags concept, as long as it's clearly separated from
> >> IntEnum -- it seems that's the consensus now.
> >>
> >> I don't like the name IntFlags: BitFlags makes more sense to me, since
> >> the key feature is supporting bitwise operators.
> >>
> >> Calling it BitFlags has the additional advantage of making it very
> >> clear that it's not closely related to IntEnum.
> >>
> >> Best,
> >>
> >> Luciano
> >>
> >> On Tue, Mar 3, 2015 at 12:52 PM, Serhiy Storchaka <storchaka at gmail.com>
> >> wrote:
> >> > Enum and IntEnum classes allow constants to have nice str() and repr()
> >> > representations.
> >> >
> >> >>>> socket.AF_INET
> >> > <AddressFamily.AF_INET: 2>
> >> >>>> socket.socket()
> >> > <socket.socket fd=3, family=AddressFamily.AF_INET,
> >> > type=SocketKind.SOCK_STREAM, proto=0, laddr=('0.0.0.0', 0)>
> >> >
> >> > But when integer constants are flags that should be ORed, IntEnum
> >> > doesn't
> >> > help, because the result of bitwise OR of two IntEnum instances is
> int,
> >> > and
> >> > this value can't be represented as IntEnum.
> >> >
> >> > We need new type IntFlags. It is like IntEnum, but has differences:
> >> >
> >> > 1. The value of an instance should be not limited to the set of
> >> > predefined
> >> > constants. It can be a combination of predefined constants or even
> >> > arbitrary
> >> > integer.
> >> >
> >> > 2. The result of "|", "&" and "~" operators for IntFlags arguments
> >> > should be
> >> > an instance of the same IntFlags subclass.
> >> >
> >> > 3. It should have nice str() and repr().
> >> >
> >> >>>> print(stat.S_IROTH | stat.S_IWOTH)
> >> > stat.S_IROTH|stat.S_IWOTH
> >> >>>> stat.S_IROTH | stat.S_IWOTH
> >> > <StatFlags.S_IROTH|S_IWOTH: 6>
> >> >
> >> > Any thoughts?
> >> >
> >> > _______________________________________________
> >> > Python-ideas mailing list
> >> > Python-ideas at python.org
> >> > https://mail.python.org/mailman/listinfo/python-ideas
> >> > Code of Conduct: http://python.org/psf/codeofconduct/
> >>
> >>
> >>
> >> --
> >> Luciano Ramalho
> >> Twitter: @ramalhoorg
> >>
> >> Professor em: http://python.pro.br
> >> Twitter: @pythonprobr
> >> _______________________________________________
> >> Python-ideas mailing list
> >> Python-ideas at python.org
> >> https://mail.python.org/mailman/listinfo/python-ideas
> >> Code of Conduct: http://python.org/psf/codeofconduct/
> >
> >
>
>
>
> --
> Luciano Ramalho
> Twitter: @ramalhoorg
>
> Professor em: http://python.pro.br
> Twitter: @pythonprobr
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>



-- 

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150305/85e41326/attachment.html>


More information about the Python-ideas mailing list