You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

624 lines
18 KiB

bpo-38250: [Enum] single-bit flags are canonical (GH-24215) Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags. Iterating, repr(), and str() show members in definition order. When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired: >>> class Test(Flag, boundary=CONFORM): ... ONE = 1 ... TWO = 2 ... >>> Test(5) <Test.ONE: 1> Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits. Iteration is now in member definition order. If member definition order matches increasing value order, then a more efficient method of flag decomposition is used; otherwise, sort() is called on the results of that method to get definition order. ``re`` module: repr() has been modified to support as closely as possible its previous output; the big difference is that inverted flags cannot be output as before because the inversion operation now always returns the comparable positive result; i.e. re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG) in both of the above terms, the ``value`` is 282. re's tests have been updated to reflect the modifications to repr().
5 years ago
bpo-38250: [Enum] single-bit flags are canonical (GH-24215) Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags. Iterating, repr(), and str() show members in definition order. When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired: >>> class Test(Flag, boundary=CONFORM): ... ONE = 1 ... TWO = 2 ... >>> Test(5) <Test.ONE: 1> Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits. Iteration is now in member definition order. If member definition order matches increasing value order, then a more efficient method of flag decomposition is used; otherwise, sort() is called on the results of that method to get definition order. ``re`` module: repr() has been modified to support as closely as possible its previous output; the big difference is that inverted flags cannot be output as before because the inversion operation now always returns the comparable positive result; i.e. re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG) in both of the above terms, the ``value`` is 282. re's tests have been updated to reflect the modifications to repr().
5 years ago
bpo-38250: [Enum] single-bit flags are canonical (GH-24215) Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags. Iterating, repr(), and str() show members in definition order. When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired: >>> class Test(Flag, boundary=CONFORM): ... ONE = 1 ... TWO = 2 ... >>> Test(5) <Test.ONE: 1> Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits. Iteration is now in member definition order. If member definition order matches increasing value order, then a more efficient method of flag decomposition is used; otherwise, sort() is called on the results of that method to get definition order. ``re`` module: repr() has been modified to support as closely as possible its previous output; the big difference is that inverted flags cannot be output as before because the inversion operation now always returns the comparable positive result; i.e. re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG) in both of the above terms, the ``value`` is 282. re's tests have been updated to reflect the modifications to repr().
5 years ago
bpo-38250: [Enum] single-bit flags are canonical (GH-24215) Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags. Iterating, repr(), and str() show members in definition order. When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired: >>> class Test(Flag, boundary=CONFORM): ... ONE = 1 ... TWO = 2 ... >>> Test(5) <Test.ONE: 1> Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits. Iteration is now in member definition order. If member definition order matches increasing value order, then a more efficient method of flag decomposition is used; otherwise, sort() is called on the results of that method to get definition order. ``re`` module: repr() has been modified to support as closely as possible its previous output; the big difference is that inverted flags cannot be output as before because the inversion operation now always returns the comparable positive result; i.e. re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG) in both of the above terms, the ``value`` is 282. re's tests have been updated to reflect the modifications to repr().
5 years ago
bpo-38250: [Enum] single-bit flags are canonical (GH-24215) Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags. Iterating, repr(), and str() show members in definition order. When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired: >>> class Test(Flag, boundary=CONFORM): ... ONE = 1 ... TWO = 2 ... >>> Test(5) <Test.ONE: 1> Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits. Iteration is now in member definition order. If member definition order matches increasing value order, then a more efficient method of flag decomposition is used; otherwise, sort() is called on the results of that method to get definition order. ``re`` module: repr() has been modified to support as closely as possible its previous output; the big difference is that inverted flags cannot be output as before because the inversion operation now always returns the comparable positive result; i.e. re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG) in both of the above terms, the ``value`` is 282. re's tests have been updated to reflect the modifications to repr().
5 years ago
bpo-38250: [Enum] single-bit flags are canonical (GH-24215) Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags. Iterating, repr(), and str() show members in definition order. When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired: >>> class Test(Flag, boundary=CONFORM): ... ONE = 1 ... TWO = 2 ... >>> Test(5) <Test.ONE: 1> Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits. Iteration is now in member definition order. If member definition order matches increasing value order, then a more efficient method of flag decomposition is used; otherwise, sort() is called on the results of that method to get definition order. ``re`` module: repr() has been modified to support as closely as possible its previous output; the big difference is that inverted flags cannot be output as before because the inversion operation now always returns the comparable positive result; i.e. re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG) in both of the above terms, the ``value`` is 282. re's tests have been updated to reflect the modifications to repr().
5 years ago
bpo-38250: [Enum] single-bit flags are canonical (GH-24215) Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags. Iterating, repr(), and str() show members in definition order. When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired: >>> class Test(Flag, boundary=CONFORM): ... ONE = 1 ... TWO = 2 ... >>> Test(5) <Test.ONE: 1> Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits. Iteration is now in member definition order. If member definition order matches increasing value order, then a more efficient method of flag decomposition is used; otherwise, sort() is called on the results of that method to get definition order. ``re`` module: repr() has been modified to support as closely as possible its previous output; the big difference is that inverted flags cannot be output as before because the inversion operation now always returns the comparable positive result; i.e. re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG) in both of the above terms, the ``value`` is 282. re's tests have been updated to reflect the modifications to repr().
5 years ago
bpo-38250: [Enum] single-bit flags are canonical (GH-24215) Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags. Iterating, repr(), and str() show members in definition order. When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired: >>> class Test(Flag, boundary=CONFORM): ... ONE = 1 ... TWO = 2 ... >>> Test(5) <Test.ONE: 1> Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits. Iteration is now in member definition order. If member definition order matches increasing value order, then a more efficient method of flag decomposition is used; otherwise, sort() is called on the results of that method to get definition order. ``re`` module: repr() has been modified to support as closely as possible its previous output; the big difference is that inverted flags cannot be output as before because the inversion operation now always returns the comparable positive result; i.e. re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG) in both of the above terms, the ``value`` is 282. re's tests have been updated to reflect the modifications to repr().
5 years ago
bpo-38250: [Enum] single-bit flags are canonical (GH-24215) Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags. Iterating, repr(), and str() show members in definition order. When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired: >>> class Test(Flag, boundary=CONFORM): ... ONE = 1 ... TWO = 2 ... >>> Test(5) <Test.ONE: 1> Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits. Iteration is now in member definition order. If member definition order matches increasing value order, then a more efficient method of flag decomposition is used; otherwise, sort() is called on the results of that method to get definition order. ``re`` module: repr() has been modified to support as closely as possible its previous output; the big difference is that inverted flags cannot be output as before because the inversion operation now always returns the comparable positive result; i.e. re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG) in both of the above terms, the ``value`` is 282. re's tests have been updated to reflect the modifications to repr().
5 years ago
bpo-38250: [Enum] single-bit flags are canonical (GH-24215) Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags. Iterating, repr(), and str() show members in definition order. When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired: >>> class Test(Flag, boundary=CONFORM): ... ONE = 1 ... TWO = 2 ... >>> Test(5) <Test.ONE: 1> Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits. Iteration is now in member definition order. If member definition order matches increasing value order, then a more efficient method of flag decomposition is used; otherwise, sort() is called on the results of that method to get definition order. ``re`` module: repr() has been modified to support as closely as possible its previous output; the big difference is that inverted flags cannot be output as before because the inversion operation now always returns the comparable positive result; i.e. re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG) in both of the above terms, the ``value`` is 282. re's tests have been updated to reflect the modifications to repr().
5 years ago
bpo-38250: [Enum] single-bit flags are canonical (GH-24215) Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags. Iterating, repr(), and str() show members in definition order. When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired: >>> class Test(Flag, boundary=CONFORM): ... ONE = 1 ... TWO = 2 ... >>> Test(5) <Test.ONE: 1> Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits. Iteration is now in member definition order. If member definition order matches increasing value order, then a more efficient method of flag decomposition is used; otherwise, sort() is called on the results of that method to get definition order. ``re`` module: repr() has been modified to support as closely as possible its previous output; the big difference is that inverted flags cannot be output as before because the inversion operation now always returns the comparable positive result; i.e. re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG) in both of the above terms, the ``value`` is 282. re's tests have been updated to reflect the modifications to repr().
5 years ago
bpo-38250: [Enum] single-bit flags are canonical (GH-24215) Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags. Iterating, repr(), and str() show members in definition order. When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired: >>> class Test(Flag, boundary=CONFORM): ... ONE = 1 ... TWO = 2 ... >>> Test(5) <Test.ONE: 1> Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits. Iteration is now in member definition order. If member definition order matches increasing value order, then a more efficient method of flag decomposition is used; otherwise, sort() is called on the results of that method to get definition order. ``re`` module: repr() has been modified to support as closely as possible its previous output; the big difference is that inverted flags cannot be output as before because the inversion operation now always returns the comparable positive result; i.e. re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG) in both of the above terms, the ``value`` is 282. re's tests have been updated to reflect the modifications to repr().
5 years ago
bpo-38250: [Enum] single-bit flags are canonical (GH-24215) Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags. Iterating, repr(), and str() show members in definition order. When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired: >>> class Test(Flag, boundary=CONFORM): ... ONE = 1 ... TWO = 2 ... >>> Test(5) <Test.ONE: 1> Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits. Iteration is now in member definition order. If member definition order matches increasing value order, then a more efficient method of flag decomposition is used; otherwise, sort() is called on the results of that method to get definition order. ``re`` module: repr() has been modified to support as closely as possible its previous output; the big difference is that inverted flags cannot be output as before because the inversion operation now always returns the comparable positive result; i.e. re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG) in both of the above terms, the ``value`` is 282. re's tests have been updated to reflect the modifications to repr().
5 years ago
bpo-38250: [Enum] single-bit flags are canonical (GH-24215) Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags. Iterating, repr(), and str() show members in definition order. When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired: >>> class Test(Flag, boundary=CONFORM): ... ONE = 1 ... TWO = 2 ... >>> Test(5) <Test.ONE: 1> Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits. Iteration is now in member definition order. If member definition order matches increasing value order, then a more efficient method of flag decomposition is used; otherwise, sort() is called on the results of that method to get definition order. ``re`` module: repr() has been modified to support as closely as possible its previous output; the big difference is that inverted flags cannot be output as before because the inversion operation now always returns the comparable positive result; i.e. re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG) in both of the above terms, the ``value`` is 282. re's tests have been updated to reflect the modifications to repr().
5 years ago
bpo-38250: [Enum] single-bit flags are canonical (GH-24215) Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags. Iterating, repr(), and str() show members in definition order. When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired: >>> class Test(Flag, boundary=CONFORM): ... ONE = 1 ... TWO = 2 ... >>> Test(5) <Test.ONE: 1> Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits. Iteration is now in member definition order. If member definition order matches increasing value order, then a more efficient method of flag decomposition is used; otherwise, sort() is called on the results of that method to get definition order. ``re`` module: repr() has been modified to support as closely as possible its previous output; the big difference is that inverted flags cannot be output as before because the inversion operation now always returns the comparable positive result; i.e. re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG) in both of the above terms, the ``value`` is 282. re's tests have been updated to reflect the modifications to repr().
5 years ago
bpo-38250: [Enum] single-bit flags are canonical (GH-24215) Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags. Iterating, repr(), and str() show members in definition order. When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired: >>> class Test(Flag, boundary=CONFORM): ... ONE = 1 ... TWO = 2 ... >>> Test(5) <Test.ONE: 1> Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits. Iteration is now in member definition order. If member definition order matches increasing value order, then a more efficient method of flag decomposition is used; otherwise, sort() is called on the results of that method to get definition order. ``re`` module: repr() has been modified to support as closely as possible its previous output; the big difference is that inverted flags cannot be output as before because the inversion operation now always returns the comparable positive result; i.e. re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG) in both of the above terms, the ``value`` is 282. re's tests have been updated to reflect the modifications to repr().
5 years ago
bpo-38250: [Enum] single-bit flags are canonical (GH-24215) Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags. Iterating, repr(), and str() show members in definition order. When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired: >>> class Test(Flag, boundary=CONFORM): ... ONE = 1 ... TWO = 2 ... >>> Test(5) <Test.ONE: 1> Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits. Iteration is now in member definition order. If member definition order matches increasing value order, then a more efficient method of flag decomposition is used; otherwise, sort() is called on the results of that method to get definition order. ``re`` module: repr() has been modified to support as closely as possible its previous output; the big difference is that inverted flags cannot be output as before because the inversion operation now always returns the comparable positive result; i.e. re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG) in both of the above terms, the ``value`` is 282. re's tests have been updated to reflect the modifications to repr().
5 years ago
bpo-38250: [Enum] single-bit flags are canonical (GH-24215) Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags. Iterating, repr(), and str() show members in definition order. When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired: >>> class Test(Flag, boundary=CONFORM): ... ONE = 1 ... TWO = 2 ... >>> Test(5) <Test.ONE: 1> Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits. Iteration is now in member definition order. If member definition order matches increasing value order, then a more efficient method of flag decomposition is used; otherwise, sort() is called on the results of that method to get definition order. ``re`` module: repr() has been modified to support as closely as possible its previous output; the big difference is that inverted flags cannot be output as before because the inversion operation now always returns the comparable positive result; i.e. re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG) in both of the above terms, the ``value`` is 282. re's tests have been updated to reflect the modifications to repr().
5 years ago
bpo-38250: [Enum] single-bit flags are canonical (GH-24215) Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags. Iterating, repr(), and str() show members in definition order. When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired: >>> class Test(Flag, boundary=CONFORM): ... ONE = 1 ... TWO = 2 ... >>> Test(5) <Test.ONE: 1> Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits. Iteration is now in member definition order. If member definition order matches increasing value order, then a more efficient method of flag decomposition is used; otherwise, sort() is called on the results of that method to get definition order. ``re`` module: repr() has been modified to support as closely as possible its previous output; the big difference is that inverted flags cannot be output as before because the inversion operation now always returns the comparable positive result; i.e. re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG) in both of the above terms, the ``value`` is 282. re's tests have been updated to reflect the modifications to repr().
5 years ago
bpo-38250: [Enum] single-bit flags are canonical (GH-24215) Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags. Iterating, repr(), and str() show members in definition order. When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired: >>> class Test(Flag, boundary=CONFORM): ... ONE = 1 ... TWO = 2 ... >>> Test(5) <Test.ONE: 1> Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits. Iteration is now in member definition order. If member definition order matches increasing value order, then a more efficient method of flag decomposition is used; otherwise, sort() is called on the results of that method to get definition order. ``re`` module: repr() has been modified to support as closely as possible its previous output; the big difference is that inverted flags cannot be output as before because the inversion operation now always returns the comparable positive result; i.e. re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG) in both of the above terms, the ``value`` is 282. re's tests have been updated to reflect the modifications to repr().
5 years ago
bpo-38250: [Enum] single-bit flags are canonical (GH-24215) Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags. Iterating, repr(), and str() show members in definition order. When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired: >>> class Test(Flag, boundary=CONFORM): ... ONE = 1 ... TWO = 2 ... >>> Test(5) <Test.ONE: 1> Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits. Iteration is now in member definition order. If member definition order matches increasing value order, then a more efficient method of flag decomposition is used; otherwise, sort() is called on the results of that method to get definition order. ``re`` module: repr() has been modified to support as closely as possible its previous output; the big difference is that inverted flags cannot be output as before because the inversion operation now always returns the comparable positive result; i.e. re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG) in both of the above terms, the ``value`` is 282. re's tests have been updated to reflect the modifications to repr().
5 years ago
bpo-38250: [Enum] single-bit flags are canonical (GH-24215) Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags. Iterating, repr(), and str() show members in definition order. When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired: >>> class Test(Flag, boundary=CONFORM): ... ONE = 1 ... TWO = 2 ... >>> Test(5) <Test.ONE: 1> Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits. Iteration is now in member definition order. If member definition order matches increasing value order, then a more efficient method of flag decomposition is used; otherwise, sort() is called on the results of that method to get definition order. ``re`` module: repr() has been modified to support as closely as possible its previous output; the big difference is that inverted flags cannot be output as before because the inversion operation now always returns the comparable positive result; i.e. re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG) in both of the above terms, the ``value`` is 282. re's tests have been updated to reflect the modifications to repr().
5 years ago
bpo-38250: [Enum] single-bit flags are canonical (GH-24215) Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags. Iterating, repr(), and str() show members in definition order. When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired: >>> class Test(Flag, boundary=CONFORM): ... ONE = 1 ... TWO = 2 ... >>> Test(5) <Test.ONE: 1> Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits. Iteration is now in member definition order. If member definition order matches increasing value order, then a more efficient method of flag decomposition is used; otherwise, sort() is called on the results of that method to get definition order. ``re`` module: repr() has been modified to support as closely as possible its previous output; the big difference is that inverted flags cannot be output as before because the inversion operation now always returns the comparable positive result; i.e. re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG) in both of the above terms, the ``value`` is 282. re's tests have been updated to reflect the modifications to repr().
5 years ago
bpo-38250: [Enum] single-bit flags are canonical (GH-24215) Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags. Iterating, repr(), and str() show members in definition order. When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired: >>> class Test(Flag, boundary=CONFORM): ... ONE = 1 ... TWO = 2 ... >>> Test(5) <Test.ONE: 1> Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits. Iteration is now in member definition order. If member definition order matches increasing value order, then a more efficient method of flag decomposition is used; otherwise, sort() is called on the results of that method to get definition order. ``re`` module: repr() has been modified to support as closely as possible its previous output; the big difference is that inverted flags cannot be output as before because the inversion operation now always returns the comparable positive result; i.e. re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG) in both of the above terms, the ``value`` is 282. re's tests have been updated to reflect the modifications to repr().
5 years ago
bpo-38250: [Enum] single-bit flags are canonical (GH-24215) Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags. Iterating, repr(), and str() show members in definition order. When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired: >>> class Test(Flag, boundary=CONFORM): ... ONE = 1 ... TWO = 2 ... >>> Test(5) <Test.ONE: 1> Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits. Iteration is now in member definition order. If member definition order matches increasing value order, then a more efficient method of flag decomposition is used; otherwise, sort() is called on the results of that method to get definition order. ``re`` module: repr() has been modified to support as closely as possible its previous output; the big difference is that inverted flags cannot be output as before because the inversion operation now always returns the comparable positive result; i.e. re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG) in both of the above terms, the ``value`` is 282. re's tests have been updated to reflect the modifications to repr().
5 years ago
bpo-38250: [Enum] single-bit flags are canonical (GH-24215) Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags. Iterating, repr(), and str() show members in definition order. When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired: >>> class Test(Flag, boundary=CONFORM): ... ONE = 1 ... TWO = 2 ... >>> Test(5) <Test.ONE: 1> Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits. Iteration is now in member definition order. If member definition order matches increasing value order, then a more efficient method of flag decomposition is used; otherwise, sort() is called on the results of that method to get definition order. ``re`` module: repr() has been modified to support as closely as possible its previous output; the big difference is that inverted flags cannot be output as before because the inversion operation now always returns the comparable positive result; i.e. re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG) in both of the above terms, the ``value`` is 282. re's tests have been updated to reflect the modifications to repr().
5 years ago
bpo-38250: [Enum] single-bit flags are canonical (GH-24215) Flag members are now divided by one-bit verses multi-bit, with multi-bit being treated as aliases. Iterating over a flag only returns the contained single-bit flags. Iterating, repr(), and str() show members in definition order. When constructing combined-member flags, any extra integer values are either discarded (CONFORM), turned into ints (EJECT) or treated as errors (STRICT). Flag classes can specify which of those three behaviors is desired: >>> class Test(Flag, boundary=CONFORM): ... ONE = 1 ... TWO = 2 ... >>> Test(5) <Test.ONE: 1> Besides the three above behaviors, there is also KEEP, which should not be used unless necessary -- for example, _convert_ specifies KEEP as there are flag sets in the stdlib that are incomplete and/or inconsistent (e.g. ssl.Options). KEEP will, as the name suggests, keep all bits; however, iterating over a flag with extra bits will only return the canonical flags contained, not the extra bits. Iteration is now in member definition order. If member definition order matches increasing value order, then a more efficient method of flag decomposition is used; otherwise, sort() is called on the results of that method to get definition order. ``re`` module: repr() has been modified to support as closely as possible its previous output; the big difference is that inverted flags cannot be output as before because the inversion operation now always returns the comparable positive result; i.e. re.A|re.I|re.M|re.S is ~(re.L|re.U|re.S|re.T|re.DEBUG) in both of the above terms, the ``value`` is 282. re's tests have been updated to reflect the modifications to repr().
5 years ago
  1. :mod:`enum` --- Support for enumerations
  2. ========================================
  3. .. module:: enum
  4. :synopsis: Implementation of an enumeration class.
  5. .. moduleauthor:: Ethan Furman <ethan@stoneleaf.us>
  6. .. sectionauthor:: Barry Warsaw <barry@python.org>
  7. .. sectionauthor:: Eli Bendersky <eliben@gmail.com>
  8. .. sectionauthor:: Ethan Furman <ethan@stoneleaf.us>
  9. .. versionadded:: 3.4
  10. **Source code:** :source:`Lib/enum.py`
  11. .. sidebar:: Important
  12. This page contains the API reference information. For tutorial
  13. information and discussion of more advanced topics, see
  14. * :ref:`Basic Tutorial <enum-basic-tutorial>`
  15. * :ref:`Advanced Tutorial <enum-advanced-tutorial>`
  16. * :ref:`Enum Cookbook <enum-cookbook>`
  17. ----------------
  18. An enumeration:
  19. * is a set of symbolic names (members) bound to unique values
  20. * can be iterated over to return its members in definition order
  21. * uses :meth:`call` syntax to return members by value
  22. * uses :meth:`index` syntax to return members by name
  23. Enumerations are created either by using the :keyword:`class` syntax, or by
  24. using function-call syntax::
  25. >>> from enum import Enum
  26. >>> # class syntax
  27. >>> class Color(Enum):
  28. ... RED = 1
  29. ... GREEN = 2
  30. ... BLUE = 3
  31. >>> # functional syntax
  32. >>> Color = Enum('Color', ['RED', 'GREEN', 'BLUE'])
  33. Even though we can use the :keyword:`class` syntax to create Enums, Enums
  34. are not normal Python classes. See
  35. :ref:`How are Enums different? <enum-class-differences>` for more details.
  36. .. note:: Nomenclature
  37. - The class :class:`Color` is an *enumeration* (or *enum*)
  38. - The attributes :attr:`Color.RED`, :attr:`Color.GREEN`, etc., are
  39. *enumeration members* (or *enum members*) and are functionally constants.
  40. - The enum members have *names* and *values* (the name of
  41. :attr:`Color.RED` is ``RED``, the value of :attr:`Color.BLUE` is
  42. ``3``, etc.)
  43. Module Contents
  44. ---------------
  45. :class:`EnumType`
  46. The ``type`` for Enum and its subclasses.
  47. :class:`Enum`
  48. Base class for creating enumerated constants.
  49. :class:`IntEnum`
  50. Base class for creating enumerated constants that are also
  51. subclasses of :class:`int`.
  52. :class:`StrEnum`
  53. Base class for creating enumerated constants that are also
  54. subclasses of :class:`str`.
  55. :class:`Flag`
  56. Base class for creating enumerated constants that can be combined using
  57. the bitwise operations without losing their :class:`Flag` membership.
  58. :class:`IntFlag`
  59. Base class for creating enumerated constants that can be combined using
  60. the bitwise operators without losing their :class:`IntFlag` membership.
  61. :class:`IntFlag` members are also subclasses of :class:`int`.
  62. :class:`FlagBoundary`
  63. An enumeration with the values ``STRICT``, ``CONFORM``, ``EJECT``, and
  64. ``KEEP`` which allows for more fine-grained control over how invalid values
  65. are dealt with in an enumeration.
  66. :class:`auto`
  67. Instances are replaced with an appropriate value for Enum members.
  68. :class:`StrEnum` defaults to the lower-cased version of the member name,
  69. while other Enums default to 1 and increase from there.
  70. :func:`global_enum`
  71. :class:`Enum` class decorator to apply the appropriate global `__repr__`,
  72. and export its members into the global name space.
  73. :func:`property`
  74. Allows :class:`Enum` members to have attributes without conflicting with
  75. other members' names.
  76. :func:`unique`
  77. Enum class decorator that ensures only one name is bound to any one value.
  78. .. versionadded:: 3.6 ``Flag``, ``IntFlag``, ``auto``
  79. .. versionadded:: 3.10 ``StrEnum``
  80. Data Types
  81. ----------
  82. .. class:: EnumType
  83. *EnumType* is the :term:`metaclass` for *enum* enumerations. It is possible
  84. to subclass *EnumType* -- see :ref:`Subclassing EnumType <enumtype-examples>`
  85. for details.
  86. .. method:: EnumType.__contains__(cls, member)
  87. Returns ``True`` if member belongs to the ``cls``::
  88. >>> some_var = Color.RED
  89. >>> some_var in Color
  90. True
  91. .. method:: EnumType.__dir__(cls)
  92. Returns ``['__class__', '__doc__', '__members__', '__module__']`` and the
  93. names of the members in *cls*::
  94. >>> dir(Color)
  95. ['BLUE', 'GREEN', 'RED', '__class__', '__doc__', '__members__', '__module__']
  96. .. method:: EnumType.__getattr__(cls, name)
  97. Returns the Enum member in *cls* matching *name*, or raises an :exc:`AttributeError`::
  98. >>> Color.GREEN
  99. Color.GREEN
  100. .. method:: EnumType.__getitem__(cls, name)
  101. Returns the Enum member in *cls* matching *name*, or raises an :exc:`KeyError`::
  102. >>> Color['BLUE']
  103. Color.BLUE
  104. .. method:: EnumType.__iter__(cls)
  105. Returns each member in *cls* in definition order::
  106. >>> list(Color)
  107. [Color.RED, Color.GREEN, Color.BLUE]
  108. .. method:: EnumType.__len__(cls)
  109. Returns the number of member in *cls*::
  110. >>> len(Color)
  111. 3
  112. .. method:: EnumType.__reversed__(cls)
  113. Returns each member in *cls* in reverse definition order::
  114. >>> list(reversed(Color))
  115. [Color.BLUE, Color.GREEN, Color.RED]
  116. .. class:: Enum
  117. *Enum* is the base class for all *enum* enumerations.
  118. .. attribute:: Enum.name
  119. The name used to define the ``Enum`` member::
  120. >>> Color.BLUE.name
  121. 'BLUE'
  122. .. attribute:: Enum.value
  123. The value given to the ``Enum`` member::
  124. >>> Color.RED.value
  125. 1
  126. .. note:: Enum member values
  127. Member values can be anything: :class:`int`, :class:`str`, etc.. If
  128. the exact value is unimportant you may use :class:`auto` instances and an
  129. appropriate value will be chosen for you. Care must be taken if you mix
  130. :class:`auto` with other values.
  131. .. attribute:: Enum._ignore_
  132. ``_ignore_`` is only used during creation and is removed from the
  133. enumeration once that is complete.
  134. ``_ignore_`` is a list of names that will not become members, and whose
  135. names will also be removed from the completed enumeration. See
  136. :ref:`TimePeriod <enum-time-period>` for an example.
  137. .. method:: Enum.__call__(cls, value, names=None, \*, module=None, qualname=None, type=None, start=1, boundary=None)
  138. This method is called in two different ways:
  139. * to look up an existing member:
  140. :cls: The enum class being called.
  141. :value: The value to lookup.
  142. * to use the ``cls`` enum to create a new enum:
  143. :cls: The enum class being called.
  144. :value: The name of the new Enum to create.
  145. :names: The names/values of the members for the new Enum.
  146. :module: The name of the module the new Enum is created in.
  147. :qualname: The actual location in the module where this Enum can be found.
  148. :type: A mix-in type for the new Enum.
  149. :start: The first integer value for the Enum (used by :class:`auto`)
  150. :boundary: How to handle out-of-range values from bit operations (:class:`Flag` only)
  151. .. method:: Enum.__dir__(self)
  152. Returns ``['__class__', '__doc__', '__module__', 'name', 'value']`` and
  153. any public methods defined on *self.__class__*::
  154. >>> from datetime import date
  155. >>> class Weekday(Enum):
  156. ... MONDAY = 1
  157. ... TUESDAY = 2
  158. ... WEDNESDAY = 3
  159. ... THURSDAY = 4
  160. ... FRIDAY = 5
  161. ... SATURDAY = 6
  162. ... SUNDAY = 7
  163. ... @classmethod
  164. ... def today(cls):
  165. ... print('today is %s' % cls(date.today.isoweekday).naem)
  166. >>> dir(Weekday.SATURDAY)
  167. ['__class__', '__doc__', '__module__', 'name', 'today', 'value']
  168. .. method:: Enum._generate_next_value_(name, start, count, last_values)
  169. :name: The name of the member being defined (e.g. 'RED').
  170. :start: The start value for the Enum; the default is 1.
  171. :count: The number of members currently defined, not including this one.
  172. :last_values: A list of the previous values.
  173. A *staticmethod* that is used to determine the next value returned by
  174. :class:`auto`::
  175. >>> from enum import auto
  176. >>> class PowersOfThree(Enum):
  177. ... @staticmethod
  178. ... def _generate_next_value_(name, start, count, last_values):
  179. ... return (count + 1) * 3
  180. ... FIRST = auto()
  181. ... SECOND = auto()
  182. >>> PowersOfThree.SECOND.value
  183. 6
  184. .. method:: Enum._missing_(cls, value)
  185. A *classmethod* for looking up values not found in *cls*. By default it
  186. does nothing, but can be overridden to implement custom search behavior::
  187. >>> from enum import StrEnum
  188. >>> class Build(StrEnum):
  189. ... DEBUG = auto()
  190. ... OPTIMIZED = auto()
  191. ... @classmethod
  192. ... def _missing_(cls, value):
  193. ... value = value.lower()
  194. ... for member in cls:
  195. ... if member.value == value:
  196. ... return member
  197. ... return None
  198. >>> Build.DEBUG.value
  199. 'debug'
  200. >>> Build('deBUG')
  201. Build.DEBUG
  202. .. method:: Enum.__repr__(self)
  203. Returns the string used for *repr()* calls. By default, returns the
  204. *Enum* name and the member name, but can be overridden::
  205. >>> class OldStyle(Enum):
  206. ... RETRO = auto()
  207. ... OLD_SCHOOl = auto()
  208. ... YESTERYEAR = auto()
  209. ... def __repr__(self):
  210. ... cls_name = self.__class__.__name__
  211. ... return f'<{cls_name}.{self.name}: {self.value}>'
  212. >>> OldStyle.RETRO
  213. <OldStyle.RETRO: 1>
  214. .. method:: Enum.__str__(self)
  215. Returns the string used for *str()* calls. By default, returns the
  216. member name, but can be overridden::
  217. >>> class OldStyle(Enum):
  218. ... RETRO = auto()
  219. ... OLD_SCHOOl = auto()
  220. ... YESTERYEAR = auto()
  221. ... def __str__(self):
  222. ... cls_name = self.__class__.__name__
  223. ... return f'{cls_name}.{self.name}'
  224. >>> OldStyle.RETRO
  225. OldStyle.RETRO
  226. .. note::
  227. Using :class:`auto` with :class:`Enum` results in integers of increasing value,
  228. starting with ``1``.
  229. .. class:: IntEnum
  230. *IntEnum* is the same as *Enum*, but its members are also integers and can be
  231. used anywhere that an integer can be used. If any integer operation is performed
  232. with an *IntEnum* member, the resulting value loses its enumeration status.
  233. >>> from enum import IntEnum
  234. >>> class Numbers(IntEnum):
  235. ... ONE = 1
  236. ... TWO = 2
  237. ... THREE = 3
  238. >>> Numbers.THREE
  239. Numbers.THREE
  240. >>> Numbers.ONE + Numbers.TWO
  241. 3
  242. >>> Numbers.THREE + 5
  243. 8
  244. >>> Numbers.THREE == 3
  245. True
  246. .. note::
  247. Using :class:`auto` with :class:`IntEnum` results in integers of increasing value,
  248. starting with ``1``.
  249. .. class:: StrEnum
  250. *StrEnum* is the same as *Enum*, but its members are also strings and can be used
  251. in most of the same places that a string can be used. The result of any string
  252. operation performed on or with a *StrEnum* member is not part of the enumeration.
  253. .. note:: There are places in the stdlib that check for an exact :class:`str`
  254. instead of a :class:`str` subclass (i.e. ``type(unknown) == str``
  255. instead of ``isinstance(str, unknown)``), and in those locations you
  256. will need to use ``str(StrEnum.member)``.
  257. .. note::
  258. Using :class:`auto` with :class:`StrEnum` results in values of the member name,
  259. lower-cased.
  260. .. class:: Flag
  261. *Flag* members support the bitwise operators ``&`` (*AND*), ``|`` (*OR*),
  262. ``^`` (*XOR*), and ``~`` (*INVERT*); the results of those operators are members
  263. of the enumeration.
  264. .. method:: __contains__(self, value)
  265. Returns *True* if value is in self::
  266. >>> from enum import Flag, auto
  267. >>> class Color(Flag):
  268. ... RED = auto()
  269. ... GREEN = auto()
  270. ... BLUE = auto()
  271. >>> purple = Color.RED | Color.BLUE
  272. >>> white = Color.RED | Color.GREEN | Color.BLUE
  273. >>> Color.GREEN in purple
  274. False
  275. >>> Color.GREEN in white
  276. True
  277. >>> purple in white
  278. True
  279. >>> white in purple
  280. False
  281. .. method:: __iter__(self):
  282. Returns all contained members::
  283. >>> list(Color.RED)
  284. [Color.RED]
  285. >>> list(purple)
  286. [Color.RED, Color.BLUE]
  287. .. method:: __len__(self):
  288. Returns number of members in flag::
  289. >>> len(Color.GREEN)
  290. 1
  291. >>> len(white)
  292. 3
  293. .. method:: __bool__(self):
  294. Returns *True* if any members in flag, *False* otherwise::
  295. >>> bool(Color.GREEN)
  296. True
  297. >>> bool(white)
  298. True
  299. >>> black = Color(0)
  300. >>> bool(black)
  301. False
  302. .. method:: __or__(self, other)
  303. Returns current flag binary or'ed with other::
  304. >>> Color.RED | Color.GREEN
  305. Color.RED|Color.GREEN
  306. .. method:: __and__(self, other)
  307. Returns current flag binary and'ed with other::
  308. >>> purple & white
  309. Color.RED|Color.BLUE
  310. >>> purple & Color.GREEN
  311. 0x0
  312. .. method:: __xor__(self, other)
  313. Returns current flag binary xor'ed with other::
  314. >>> purple ^ white
  315. Color.GREEN
  316. >>> purple ^ Color.GREEN
  317. Color.RED|Color.GREEN|Color.BLUE
  318. .. method:: __invert__(self):
  319. Returns all the flags in *type(self)* that are not in self::
  320. >>> ~white
  321. 0x0
  322. >>> ~purple
  323. Color.GREEN
  324. >>> ~Color.RED
  325. Color.GREEN|Color.BLUE
  326. .. note::
  327. Using :class:`auto` with :class:`Flag` results in integers that are powers
  328. of two, starting with ``1``.
  329. .. class:: IntFlag
  330. *IntFlag* is the same as *Flag*, but its members are also integers and can be
  331. used anywhere that an integer can be used.
  332. >>> from enum import IntFlag, auto
  333. >>> class Color(IntFlag):
  334. ... RED = auto()
  335. ... GREEN = auto()
  336. ... BLUE = auto()
  337. >>> Color.RED & 2
  338. 0x0
  339. >>> Color.RED | 2
  340. Color.RED|Color.GREEN
  341. If any integer operation is performed with an *IntFlag* member, the result is
  342. not an *IntFlag*::
  343. >>> Color.RED + 2
  344. 3
  345. If a *Flag* operation is performed with an *IntFlag* member and:
  346. * the result is a valid *IntFlag*: an *IntFlag* is returned
  347. * the result is not a valid *IntFlag*: the result depends on the *FlagBoundary* setting
  348. .. note::
  349. Using :class:`auto` with :class:`IntFlag` results in integers that are powers
  350. of two, starting with ``1``.
  351. .. class:: FlagBoundary
  352. *FlagBoundary* controls how out-of-range values are handled in *Flag* and its
  353. subclasses.
  354. .. attribute:: STRICT
  355. Out-of-range values cause a :exc:`ValueError` to be raised. This is the
  356. default for :class:`Flag`::
  357. >>> from enum import Flag, STRICT
  358. >>> class StrictFlag(Flag, boundary=STRICT):
  359. ... RED = auto()
  360. ... GREEN = auto()
  361. ... BLUE = auto()
  362. >>> StrictFlag(2**2 + 2**4)
  363. Traceback (most recent call last):
  364. ...
  365. ValueError: StrictFlag: invalid value: 20
  366. given 0b0 10100
  367. allowed 0b0 00111
  368. .. attribute:: CONFORM
  369. Out-of-range values have invalid values removed, leaving a valid *Flag*
  370. value::
  371. >>> from enum import Flag, CONFORM
  372. >>> class ConformFlag(Flag, boundary=CONFORM):
  373. ... RED = auto()
  374. ... GREEN = auto()
  375. ... BLUE = auto()
  376. >>> ConformFlag(2**2 + 2**4)
  377. ConformFlag.BLUE
  378. .. attribute:: EJECT
  379. Out-of-range values lose their *Flag* membership and revert to :class:`int`.
  380. This is the default for :class:`IntFlag`::
  381. >>> from enum import Flag, EJECT
  382. >>> class EjectFlag(Flag, boundary=EJECT):
  383. ... RED = auto()
  384. ... GREEN = auto()
  385. ... BLUE = auto()
  386. >>> EjectFlag(2**2 + 2**4)
  387. 20
  388. .. attribute:: KEEP
  389. Out-of-range values are kept, and the *Flag* membership is kept. This is
  390. used for some stdlib flags:
  391. >>> from enum import Flag, KEEP
  392. >>> class KeepFlag(Flag, boundary=KEEP):
  393. ... RED = auto()
  394. ... GREEN = auto()
  395. ... BLUE = auto()
  396. >>> KeepFlag(2**2 + 2**4)
  397. KeepFlag.BLUE|0x10
  398. Utilites and Decorators
  399. -----------------------
  400. .. class:: auto
  401. *auto* can be used in place of a value. If used, the *Enum* machinery will
  402. call an *Enum*'s :meth:`_generate_next_value_` to get an appropriate value.
  403. For *Enum* and *IntEnum* that appropriate value will be the last value plus
  404. one; for *Flag* and *IntFlag* it will be the first power-of-two greater
  405. than the last value; for *StrEnum* it will be the lower-cased version of the
  406. member's name.
  407. ``_generate_next_value_`` can be overridden to customize the values used by
  408. *auto*.
  409. .. decorator:: global_enum
  410. A :keyword:`class` decorator specifically for enumerations. It replaces the
  411. :meth:`__repr__` method with one that shows *module_name*.*member_name*. It
  412. also injects the members, and their aliases, into the the global namespace
  413. they were defined in.
  414. .. decorator:: property
  415. A decorator similar to the built-in *property*, but specifically for
  416. enumerations. It allows member attributes to have the same names as members
  417. themselves.
  418. .. note:: the *property* and the member must be defined in separate classes;
  419. for example, the *value* and *name* attributes are defined in the
  420. *Enum* class, and *Enum* subclasses can define members with the
  421. names ``value`` and ``name``.
  422. .. decorator:: unique
  423. A :keyword:`class` decorator specifically for enumerations. It searches an
  424. enumeration's :attr:`__members__`, gathering any aliases it finds; if any are
  425. found :exc:`ValueError` is raised with the details::
  426. >>> from enum import Enum, unique
  427. >>> @unique
  428. ... class Mistake(Enum):
  429. ... ONE = 1
  430. ... TWO = 2
  431. ... THREE = 3
  432. ... FOUR = 3
  433. ...
  434. Traceback (most recent call last):
  435. ...
  436. ValueError: duplicate values found in <enum 'Mistake'>: FOUR -> THREE