Six provides simple utilities for wrapping over differences between Python 2 and Python 3. It is intended to support codebases that work on both Python 2 and 3 without modification. six consists of only one Python file, so it is painless to copy into a project.
The name, “six”, comes from the fact that 2*3 equals 6. Why not addition? Multiplication is more powerful, and, anyway, “five” has already been snatched away by the Zope Five project.
A boolean indicating if the code is running on Python 2.
A boolean indicating if the code is running on Python 3.
Six provides constants that may differ between Python versions. Ones ending _types are mostly useful as the second argument to isinstance or issubclass.
Possible class types. In Python 2, this encompasses old-style and new-style classes. In Python 3, this is just new-styles.
Possible types for text data. This is basestring() in Python 2 and str in Python 3.
Type for representing (Unicode) textual data. This is unicode() in Python 2 and str in Python 3.
The maximum size of a container like list or dict. This is equivalent to sys.maxsize in Python 2.6 and later (including 3.x). Note, this is temptingly similar to, but not the same as sys.maxint in Python 2. There is no direct equivalent to sys.maxint in Python 3 because its integer type has no limits aside from memory.
Here’s example usage of the module:
import six def dispatch_types(value): if isinstance(value, six.integer_types): handle_integer(value) elif isinstance(value, six.class_types): handle_class(value) elif isinstance(value, six.string_types): handle_string(value)
Python 3 renamed the attributes of several intepreter data structures. The following accessors are available. Note that the recommended way to inspect functions and methods is the stdlib inspect module.
Get the function out of unbound method meth. In Python 3, unbound methods don’t exist, so this function just returns meth unchanged. Example usage:
from six import get_unbound_function class X(object): def method(self): pass method_function = get_unbound_function(X.method)
Get the function out of method object meth.
Get the self of bound method meth.
Get the closure (list of cells) associated with func. This is equivalent to func.__closure__ on Python 2.6+ and func.func_closure on Python 2.5.
Get the code object associated with func. This is equivalent to func.__code__ on Python 2.6+ and func.func_code on Python 2.5.
Get the defaults tuple associated with func. This is equivalent to func.__defaults__ on Python 2.6+ and func.func_defaults on Python 2.5.
Get the globals of func. This is equivalent to func.__globals__ on Python 2.6+ and func.func_globals on Python 2.5.
Get the next item of iterator it. StopIteration is raised if the iterator is exhausted. This is a replacement for calling it.next() in Python 2 and next(it) in Python 3.
Check if obj can be called. Note callable has returned in Python 3.2, so using six’s version is only necessary when supporting Python 3.0 or 3.1.
Returns an iterator over dictionary‘s keys. This replaces dictionary.iterkeys() on Python 2 and dictionary.keys() on Python 3. kwargs are passed through to the underlying method.
Returns an iterator over dictionary‘s values. This replaces dictionary.itervalues() on Python 2 and dictionary.values() on Python 3. kwargs are passed through to the underlying method.
Returns an iterator over dictionary‘s items. This replaces dictionary.iteritems() on Python 2 and dictionary.items() on Python 3. kwargs are passed through to the underlying method.
Calls dictionary.iterlists() on Python 2 and dictionary.lists() on Python 3. No builtin Python mapping type has such a method; this method is intended for use with multi-valued dictionaries like Werkzeug’s. kwargs are passed through to the underlying method.
Return a method object wrapping func and bound to obj. On both Python 2 and 3, this will return a types.MethodType object. The reason this wrapper exists is that on Python 2, the MethodType constructor requires the obj‘s class to be passed.
A class for making portable iterators. The intention is that it be subclassed and subclasses provide a __next__ method. In Python 2, Iterator has one method: next. It simply delegates to __next__. An alternate way to do this would be to simply alias next to __next__. However, this interacts badly with subclasses that override __next__. Iterator is empty on Python 3. (In fact, it is just aliased to object.)
These functions smooth over operations which have different syntaxes between Python 2 and 3.
Execute code in the scope of globals and locals. code can be a string or a code object. If globals or locals are not given, they will default to the scope of the caller. If just globals is given, it will also be used as locals.
Print args into file. Each argument will be separated with sep and end will be written to the file after the last argument is printed.
In Python 2, this function imitates Python 3’s print() by not having softspace support. If you don’t know what that is, you’re probably ok. :)
Reraise an exception, possibly with a different traceback. In the simple case, reraise(*sys.exc_info()) with an active exception (in an except block) reraises the current exception with the last traceback. A different traceback can be specified with the exc_traceback parameter.
Create a new class with base classes bases and metaclass metaclass. This is designed to be used in class declarations like this:
from six import with_metaclass class Meta(type): pass class Base(object): pass class MyClass(with_metaclass(Meta, Base)): pass
Another way to set a metaclass on a class is with the add_metaclass() decorator.
Class decorator that replaces a normally-constructed class with a metaclass-constructed one. Unlike with_metaclass(), add_metaclass() does not create an intermediate base class between the class being created and its bases. Example usage:
@add_metaclass(Meta) class MyClass(object): pass
That code produces a class equivalent to
class MyClass(object, metaclass=Meta): pass
on Python 3 or
class MyClass(object): __metaclass__ = MyMeta
on Python 2.
Note that class decorators require Python 2.6. However, the effect of the decorator can be emulated on Python 2.5 like so:
class MyClass(object): pass MyClass = add_metaclass(Meta)(MyClass)
Python 3 enforces the distinction between byte strings and text strings far more rigoriously than Python 2 does; binary data cannot be automatically coerced to or from text data. six provides several functions to assist in classifying string data in all Python versions.
A “fake” bytes literal. data should always be a normal string literal. In Python 2, b() returns a 8-bit string. In Python 3, data is encoded with the latin-1 encoding to bytes.
A “fake” unicode literal. text should always be a normal string literal. In Python 2, u() returns unicode, and in Python 3, a string. Also, in Python 2, the string is decoded with the unicode-escape codec, which allows unicode escapes to be used in it.
In Python 3.3, the u prefix has been reintroduced. Code that only supports Python 3 versions greater than 3.3 thus does not need u().
Converts i to a byte. i must be in range(0, 256). This is equivalent to chr() in Python 2 and bytes((i,)) in Python 3.
Converts the first byte of bs to an integer. This is equivalent to ord(bs) on Python 2 and bs on Python 3.
Return the byte at index i of buf as an integer. This is equivalent to indexing a bytes object in Python 3.
Return an iterator over bytes in buf as integers. This is equivalent to a bytes object iterator in Python 3.
Python 3 reorganized the standard library and moved several functions to different modules. Six provides a consistent interface to them through the fake six.moves module. For example, to load the module for parsing HTML on Python 2 or 3, write:
from six.moves import html_parser
Similarly, to get the function to reload modules, which was moved from the builtin module to the imp module, use:
from six.moves import reload_module
For the most part, six.moves aliases are the names of the modules in Python 3. When the new Python 3 name is a package, the components of the name are separated by underscores. For example, html.parser becomes html_parser. In some cases where several modules have been combined, the Python 2 name is retained. This is so the appropiate modules can be found when running on Python 2. For example, BaseHTTPServer which is in http.server in Python 3 is aliased as BaseHTTPServer.
Some modules which had two implementations have been merged in Python 3. For example, cPickle no longer exists in Python 3; it was merged with pickle. In these cases, fetching the fast version will load the fast one on Python 2 and the merged module in Python 3.
The urllib, urllib2, and urlparse modules have been combined in the urllib package in Python 3. The six.moves.urllib package is a version-independent location for this functionality; its structure mimics the structure of the Python 3 urllib package.
In order to make imports of the form:
from six.moves.cPickle import loads
work, six places special proxy objects in in sys.modules. These proxies lazily load the underlying module when an attribute is fetched. This will fail if the underlying module is not available in the Python interpreter. For example, sys.modules["six.moves.winreg"].LoadKey would fail on any non-Windows platform. Unfortunately, some applications try to load attributes on every module in sys.modules. six mitigates this problem for some applications by pretending attributes on unimportable modules don’t exist. This hack doesn’t work in every case, though. If you are encountering problems with the lazy modules and don’t use any from imports directly from six.moves modules, you can workaround the issue by removing the six proxy modules:
d = [name for name in sys.modules if name.startswith("six.moves.")] for name in d: del sys.modules[name]
|Name||Python 2 name||Python 3 name|
Contains functions from Python 3’s urllib.parse and Python 2’s:
Contains exceptions from Python 3’s urllib.error and Python 2’s:
Contains items from Python 3’s urllib.request and Python 2’s:
Contains classes from Python 3’s urllib.response and Python 2’s:
It is possible to add additional names to the six.moves namespace.
Instances of the following classes can be passed to add_move(). Neither have any public members.
Create a mapping for six.moves called name that references different modules in Python 2 and 3. old_mod is the name of the Python 2 module. new_mod is the name of the Python 3 module.
Create a mapping for six.moves called name that references different attributes in Python 2 and 3. old_mod is the name of the Python 2 module. new_mod is the name of the Python 3 module. If new_attr is not given, it defaults to old_attr. If neither is given, they both default to name.