Live data from Hacker News

Wield Python's super() like a Jedi

rhettinger.wordpress.com

1–10 of 67 posts

Re: Wield Python's super() like a Jedi

#4
post #3

Have to deal with enough languages as it is. Don't want to further add to my brain mess by learning Python 3 syntax just yet.

super() was introduced in Python2.2 many years ago. Everything in the article works with very old versions of Python.

Here's a link to the Python 2 version of the examples: http://code.activestate.com/recipes/577721-how-to-use-super-...

Re: Wield Python's super() like a Jedi

#7
post #6

how does the runtime performance of super() compare to the hardcoding method? (from your example, using dict.__setitem__ as opposed to super().__setitem__ )

I don't think it suffers much as the method resolution order (mro) is computed ahead-of-time when creating the class.

Re: Wield Python's super() like a Jedi

#8
post #7
post #6

how does the runtime performance of super() compare to the hardcoding method? (from your example, using dict.__setitem__ as opposed to super().__setitem__ )

I don't think it suffers much as the method resolution order (mro) is computed ahead-of-time when creating the class.

so super() is computed ahead of time? That doesn't make sense when you look at his comment:

"The calculation depends on both the class where super is called and on the instance’s tree of ancestors."

I agree that the class's MRO can be computed ahead of time, but theoretically the instance's order could be different (if a class method changed the definition of __setitem__)

Re: Wield Python's super() like a Jedi

#9
post #4
post #3

Have to deal with enough languages as it is. Don't want to further add to my brain mess by learning Python 3 syntax just yet.

super() was introduced in Python2.2 many years ago. Everything in the article works with very old versions of Python. Here's a link to the Python 2 version of the examples: http://code.activestate.com/recipes/577721-how-to-use-super-...

Python 2.x doesn't have parameterless super() so apparently most of the article's code doesn't work with old versions.

Re: Wield Python's super() like a Jedi

#10
post #6

how does the runtime performance of super() compare to the hardcoding method? (from your example, using dict.__setitem__ as opposed to super().__setitem__ )

In CPython, calling super() has about the same cost as calling any other builtin like len() or int(). The MRO itself is precomputed and stored in inst.__class__.__mro__.

Contrasting dict.__setitem__ with super().__setitem__, the latter adds one function call. In addition, both forms require a builtin lookup and an attribute lookup.

In PyPy, much of the overhead of builtin lookups, function calls, and attribute lookups is automatically optimized away.

Post reply on HN