[Python-Dev] Comparing UUID objects to strings: why not?
Hello everyone I have a usecase where I'm comparing a UUID instance with string quite often. A case where I have to convert the UUID instance to a string using str(uuid_obj), leading to redundant code: if str(uuid_obj) == uuid: # do stuff... In my experience I can't find a case where you wouldn't want the implicit conversion to string before comparing a UUID with string. So I would like to propose improving the __eq__ method of UUID to: def __eq__(self, other): if isinstance(other, UUID): return self.int == other.int elif isinstance(other, str): return str(self) == other return NotImplemented Based on my testing and experience I don't think this is a breaking change and would be a meaningful change to the way UUID works. Of course please don't hesitate to change my view or point out any flaws in my reasoning. If other people support this change I'd start the work of creating an issue and PR to get this change implemented. -- Michael Anckaert +32 474 066 467 https://www.sinax.be ___ Python-Dev mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/[email protected]/message/VXLJMWZRZLSFZRNHUITSLAV2O363WG5Q/ Code of Conduct: http://python.org/psf/codeofconduct/
[Python-Dev] Re: Comparing UUID objects to strings: why not?
Thank you all for your feedback, you all make very good points. I'll take a deeper look at the resources given. Bernardo Sulzbach writes: > I think that if the other object is a string, trying to get a UUID > from it to compare UUIDs makes more sense than converting the UUID to > string and comparing strings. Several different strings are perfectly > fine for uniquely identifying the same UUID, so you seem to have > gotten this bit backward, as Tal Einat pointed out. > > However, I strongly oppose automatic conversions, and, by proxy, this idea. > ___ > Python-Dev mailing list -- [email protected] > To unsubscribe send an email to [email protected] > https://mail.python.org/mailman3/lists/python-dev.python.org/ > Message archived at > https://mail.python.org/archives/list/[email protected]/message/AHACOVSPOVRJ257P2XSPPDA7I6AKE5IJ/ > Code of Conduct: http://python.org/psf/codeofconduct/ -- Michael Anckaert +32 474 066 467 https://www.sinax.be ___ Python-Dev mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/[email protected]/message/3V7UWQSPDAIEVPWQJLE5MTFSZZE75A2H/ Code of Conduct: http://python.org/psf/codeofconduct/
