LWN.net Logo

Avoid Version Numbers

Avoid Version Numbers

Posted Feb 15, 2011 1:48 UTC (Tue) by ldo (subscriber, #40946)
Parent article: Moving to Python 3

I hate version numbers. Don’t check for version numbers; instead, check for something as close as possible to the actual functionality you need.

Python even helps you, by providing ways to query it for things. E.g. to check for the bytes function, you don’t have to go through the rigmarole of watching for a NameError exception; just write

hasattr(__builtins__, "bytes")

What could be simpler? And similarly the PYTHON3 version check could be replaced with something like

not hasattr(__builtins__, "unichr")

(Negated so it’s True for Python 3.x and False for earlier versions.)


(Log in to post comments)

Avoid Version Numbers

Posted Feb 15, 2011 14:20 UTC (Tue) by Webexcess (subscriber, #197) [Link]

I was actually using:

    not str is bytes

But when you really mean "is this python3" then that's not clear to someone reading the code so I changed it.

Avoid Version Numbers

Posted Mar 8, 2011 22:55 UTC (Tue) by engla (guest, #47454) [Link]

Ironically, __builtins__ is officially an implementation detail of CPython. Recommended way to find the module containing the built-in functions on Python2 is: import __builtin__; on Python 3: import builtins.

Copyright © 2013, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds