This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author killerrex
Recipients barry, eli.bendersky, ethan.furman, killerrex
Date 2018-05-08.10:24:06
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1525775047.16.0.682650639539.issue33437@psf.upfronthosting.co.za>
In-reply-to
Content
I see, with mixed types you need to use __new__ to construct the elements (I imagine is specially important for non mutable types)

I have modified the example of the coordinates to try to use a mixed type. Is not the most useful thing, but it mix the bytes class.
Is it not obvious how to correctly use the mixin + __new__ operator so it is easy that the example is not correct or can be done better.

class Coordinate(bytes, Enum):
    """
    Coordinate with binary codes that can be indexed by the int code.
    """
    def __new__(cls, value, label, unit):
        obj = bytes.__new__(cls, [value])
        obj._value_ = value
        obj.label = label
        obj.unit = unit
        return obj

    PX = (0, 'P.X', 'km')
    PY = (1, 'P.Y', 'km')
    VX = (2, 'V.X', 'km/s')
    VY = (3, 'V.Y', 'km/s')


# This works as expected
for element in Coordinate:
    print("{0}: {0.label}[{0.unit}] = {0.value}".format(element))

# And this Work
print("Obtain P.Y from the name:", Coordinate['PY'])
print("Obtain V.Y from the index:", Coordinate(3))

# This shall be False
print('Coordinate.PY == 1 is:', Coordinate.PY == 1)
# But this shall be True
print("Coordinate.PY == b'\\01' is:", Coordinate.PY == b'\01')

Thanks!
History
Date User Action Args
2018-05-08 10:24:07killerrexsetrecipients: + killerrex, barry, eli.bendersky, ethan.furman
2018-05-08 10:24:07killerrexsetmessageid: <1525775047.16.0.682650639539.issue33437@psf.upfronthosting.co.za>
2018-05-08 10:24:07killerrexlinkissue33437 messages
2018-05-08 10:24:06killerrexcreate