On Mon, Oct 11, 2021 at 10:59 PM Rob Cliffe via Python-ideas <python-ideas@python.org> wrote: > > > > On 10/10/2021 13:31, Steven D'Aprano wrote: > > On Sat, Oct 09, 2021 at 08:16:58PM -0600, Finn Mason wrote: > > > >> import sys > >> if sys.version_info < (3, 6): > >> # Yell at the user > > Please, version checking is usually an anti-pattern! You should use > > feature detection whenever possible, not version checking. > > > > > Understood. > But would you agree that if you are writing code to be Python 2- and > Python 3-compatible, it is reasonable to check the major version: > > Python3 = sys.version_info.major >= 3 > .... > if Python3: > # this code used to work without this bit > ... > if not Python3: > # can't use this feature > etc. >
Maybe, but I still tend to feature-test. For instance: if str is bytes: def encode(text): return text else: def encode(text): return text.encode("UTF-8") In any case, there's not a lot of need to support Python 2 any more, so most of this sort of check doesn't exist in my code any more. ChrisA _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/T56L37EVNQPKOYGQD6P2EVMAVCJE2JFA/ Code of Conduct: http://python.org/psf/codeofconduct/