14 Advanced Python Features

61 points by knl


spookylukey

The “Proxy Properties” idea doesn’t really work. It appears to work in the example given only because print() calls repr() on a object.

If you try something like:

c.value + "xxx"

it just throws a TypeError, because c.value is not a string.

The author recognises it’s not production code. The fuller version linked to depends on lazy-object-proxy to do the hard bit here, which is making a wrapper object that appears to behave like whatever it is wrapping. It’s also the kind of thing you should really avoid if at all possible.

Halkcyon
  1. Typing Overloads

I think this is actually an anti-pattern in Python. You can’t create real overloads, it’s just sugar for the type checker, but the implementations still end up very messy. You’re better off creating separate functions for separate types.

  1. Protocols

Additional quick tip: Add the @runtime_checkable decorator if you want isinstance() checks to work alongside your Protocols!

Oooo. That is very neat.

  1. Python Futures

I wonder why Python made two Future types that aren’t compatible with each other? It seems asyncio.Future is also bound to the running event loop?

  1. Proxy Properties

My experiences with these in libraries have been pretty poor as the type checkers don’t like them.


Some cool features mentioned, but a lot of them don’t feel very advanced to a day-to-day Python developer.