Python 2 and 3 compatibility utilities
—
Core constants and type definitions for Python version detection and cross-version type checking. These utilities enable writing code that works consistently across Python 2 and 3 by providing unified type constants and version detection flags.
Boolean constants that identify the Python version at runtime, enabling version-specific code paths.
PY2: bool # True if running on Python 2, False otherwise
PY3: bool # True if running on Python 3, False otherwise
PY34: bool # True if running on Python 3.4 or higher, False otherwiseUsage Examples:
import six
if six.PY2:
# Python 2 specific code
import cPickle as pickle
elif six.PY3:
# Python 3 specific code
import pickle
# Feature detection for Python 3.4+ features
if six.PY34:
from importlib.util import spec_from_loader
else:
spec_from_loader = NoneTuple constants containing the appropriate types for cross-version isinstance() checks.
string_types: tuple # String types tuple for isinstance() checks
integer_types: tuple # Integer types tuple for isinstance() checks
class_types: tuple # Class types tuple for isinstance() checksType Details:
string_types: (str,) in Python 3, (basestring,) in Python 2integer_types: (int,) in Python 3, (int, long) in Python 2class_types: (type,) in Python 3, (type, types.ClassType) in Python 2Usage Examples:
import six
def process_string(value):
if isinstance(value, six.string_types):
return value.upper()
raise TypeError("Expected string type")
def process_number(value):
if isinstance(value, six.integer_types):
return value * 2
raise TypeError("Expected integer type")Direct references to the appropriate type for each Python version.
text_type: type # Text string type (str in PY3, unicode in PY2)
binary_type: type # Binary string type (bytes in PY3, str in PY2)Usage Examples:
import six
def ensure_unicode(value):
if isinstance(value, six.text_type):
return value
return value.decode('utf-8')
def ensure_bytes(value):
if isinstance(value, six.binary_type):
return value
return value.encode('utf-8')Cross-version constant for the maximum value an integer can hold.
MAXSIZE: int # Maximum value a variable of integer type can takeThis constant provides sys.maxsize equivalent across Python versions, handling platform-specific differences including Jython compatibility.
Usage Example:
import six
def check_large_number(value):
if value > six.MAXSIZE:
raise OverflowError("Number too large")
return valueImport specification utility available in Python 3.4+.
spec_from_loader: Callable | None # importlib.util.spec_from_loader in PY34+, None otherwiseThis provides access to importlib.util.spec_from_loader when available, or None for older Python versions.
Usage Example:
import six
if six.spec_from_loader is not None:
# Use modern import specification
spec = six.spec_from_loader("mymodule", loader)
else:
# Fallback for older Python versions
spec = None__author__: str # "Benjamin Peterson <benjamin@python.org>"
__version__: str # "1.17.0"These constants provide access to the six library's metadata for version checking and attribution purposes.
Install with Tessl CLI
npx tessl i tessl/pypi-six