3 ms·
Ahh, so Ruby doesn't - and can't[1] - support constant folding. In CPython, because monkey-patching like this isn't supported, the byte compiler turns a "0/2"
by eesmith 4y ago
Ahh, so Ruby doesn't - and can't[1] - support constant folding.
In CPython, because monkey-patching like this isn't supported, the byte compiler turns a "0/2" into a "0.0", as you can see with:
>>> def f():
... return 0 / 2
...
>>> import dis
>>> dis.dis(f)
2 0 LOAD_CONST 1 (0.0)
2 RETURN_VALUE
[1] EDIT: except through something like whole-program analysis.
- languageserver 4y ago> Ahh, so Ruby doesn't - and can't[1] - support constant folding. it is because most "OOP" languages are not really "object" languages. Ruby is a "proper" everthing is an object language, which is why everyone loves it so much even if they do not think about it.
- Spivak 4y agoHuh? Python is an everything-is-an-object language too. Supporting monkey-patching is a separate thing.
- nomel 4y agoI didn't realize this. I thought things like int's were special. I just tried x = 5 dir(x) Output: ['__abs__', '__add__', '__and__', '__bool__', ...
- eesmith 4y agoSome Python objects are open for modification, and some are closed. Closed objects don't support this sort of monkey-patching. Integers, strings, and other class objects defined in C are closed for modification. >>> x = 5 >>> x.__add__ = lambda x, y: x*y Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'int' object attribute '__add__' is read-only In that sense they are "special" (or class objects defined in Python are special). But that doesn't make a language with closed classes non-OO.