> Python is harder to JIT because of the way __dict__ works.
Heh, how about how `__class__` works? Not that I recommend actually doing this, but you can literally change the type of an already instantiated object by reassigning `__class__`:
>>> class Dog:
... def hi(self):
... print("woof")
...
>>> class Cat:
... def hi(self):
... print("meow")
...
>>> dog = Dog()
>>> dog.hi()
woof
>>> dog.__class__ = Cat
>>> dog.hi()
meow
>>> isinstance(dog, Dog)
False
>>> isinstance(dog, Cat)
True
>>>
I've been doing things in Python for over 20 years. Only recently I learn that I can do this.
This is wild. Is it part of Python's specification, or an implementation detail of CPython?
As far as I understand, CPython implementation is THE specification. Some billions dollar ERP system firmly rely on this behavior, btw.
That's also possible in JS with __proto__. The prototypes of the classes in the chain can be mutated at runtime too. How to handle these cases is well understood for JITs though and python could do the same.
See also the warning here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
And this related article:
https://mathiasbynens.be/notes/prototypes
The thing that actually makes it harder in Python is threading, because it breaks all ability to reason locally about what might happen and optimize. In Python you have to assume that another thread may do these things at any point mid-execution.
Lua has the same property with metatables. LuaJIT, which is very fast, has a solution: if you change a metatable after assigning it, or reassign a metatable, Mike Pall turns his back on you. Your code dies slowly, in the outer darkness of the interpreter loop, never to be JIT’ted upon again.
Smalltalk, Self and Lisp Machines enter the bar, with live image changes.