Python and the need for speed

Rick Johnson rantingrickjohnson at
Sun Apr 9 22:23:04 EDT 2017

On Sunday, April 9, 2017 at 4:05:57 AM UTC-5, Chris Angelico wrote:
> On Sun, Apr 9, 2017 at 5:39 PM, Steven D'Aprano <steve at> wrote:

> > Even better would be if the compiler was smart enough to
> > use the optimized, fast runtime when the dynamic features
> > aren't used, and fall back on a slower implementation only
> > when needed to support the more dynamic features.
> Yes. As long as it can know when the more dynamic features
> are being used - and that's the hard part. Remember, as
> soon as you have a single class implemented in Python, it
> could have a method injected into it without your
> knowledge. Can you detect that statically, or can you use
> the versioning of __dict__ to notice that something's been
> broken? What makes you fall back to the default
> implementation?

And all that sleuthing is slow as hell! The simple solution
is to use a declaration (aka: "strict" in JS) instead. The
programmer should be intelligent enough to know whether the
code is written in an "optimized manner", or not.

> The problem is the same thing that gives Python a lot of
> its beauty: that there's very little difference between
> built-in types and user-defined types. In CPython, types
> implemented in C are immutable, but other than that,
> they're basically the same thing as anything you make, and
> you can inspect them and stuff:
> >>> int.__add__(5, 7)
> 12

Who the hell wants to call dunder methods? Self-flagellate

> Personally, I think that would be a far better avenue to go
> down. I'm not sure it would be possible to retrofit this to
> Python (for example, there are guarantees about object
> identity that apply to *all objects*), but making the core
> immutable types into value-only non-objects wouldn't break
> a lot of code, and might provide for some significant
> performance improvements to string manipulation and
> arithmetic operations. But at some point, it's not really
> Python any more.

Well, go tell that to the type-hints shills!

More information about the Python-list mailing list