Python 3 upgrade

About ten years ago, Guido Van Rossum, the Python author and Benevolent Dictator for Life (BDFL), along with the Python community, decided to make several concurrent backward incompatible changes to Python 2.5 and release a new version, Python 3.0. The main changes were:

  • Using unicode strings as default, with the old string type becoming a full featured binary type
  • Changing several builtins, for example
    • The print statement became a function, allowing more consistent syntax and the use of the word print as a name
    • The confusing input removed, and raw_input now renamed to input
    • Simpler exec
    • Division is now split between / float division and // truncating division
  • Improved exception tracing, with chaining
  • Improved function call syntax with annotations and keyword only arguments, replacing little used tuple parameter unpacking
  • More class constructor features, such as nicer metaclass syntax, keyword arguments, __prepare__
  • Renamed standard libraries, to be more consistent
  • Removal of a lot of depreciated features, including old-style classes
  • Removal of a lot of depreciated syntax that had become learner stumbling blocks
  • Adding nonlocal variables
  • Extended tuple unpacking, like first, *rest = makes_a_tuple()
  • Removing the proliferation of .pyc files, instead using __pycache__ directories
  • Automatic selection of C-based standard library modules over pure Python ones if available
  • Unified the int and long types into one unlimited length integer type
[Read More]

A simple introduction to asyncio

This is a simple explanation of the asyncio module and new supporting language features in Python 3.5. Even though the new keywords async and await are new language constructs, they are mostly1 useless without an event loop, and that is supplied in the standard library as asyncio. Also, you need awaitable functions, which are only supplied by asyncio (or in the growing set of async libraries, like asyncssh, quamash etc.).

[Read More]

A little example of how asyncio works

This is a simple example to show how Asyncio works without using Asyncio itself, instead using a basic and poorly written event loop. This is only meant to give a flavor of what Asyncio does behind the curtains. I’m avoiding most details of the library design, like callbacks, just to keep this simple. Since this is written as an illustration, rather than real code, I’m going to dispense with trying to keep it 2.7 compatible.

[Read More]

Slots in Python

Slots seem to be poorly documented. What they do is simple, but whether they are used is tricky. This is a little mini-post on slots.

[Read More]

Basics of metaclasses

This is a quick tutorial over the basics of what metaclasses do.

The Metaclass

Metaclasses, while seemingly a complex topic, really just do something very simple. They control what happens when you have code that turns into a class object. The normal place they are executed is right after the class statement. Let’s see that in action by using print as our metaclass.

[Read More]