3 ms·
Wow, this is amazing research! I had no idea the rules actually varied this much. The same like one is especially odd. The LOAD_FAST seems to make sense because
by makmanalp 9y ago
Wow, this is amazing research! I had no idea the rules actually varied this much. The same like one is especially odd. The LOAD_FAST seems to make sense because it's defined as : "Pushes a reference to the local co_varnames[var_num] onto the stack." (emphasis mine).
Also see the global case:
In [2]: x = 500
In [7]: def wat_wat_wat():
...: global x
...: x = 600
...: return x is 600
...:
In [8]: dis.disassemble(wat_wat_wat.__code__)
3 0 LOAD_CONST 1 (600)
2 STORE_GLOBAL 0 (x)
4 4 LOAD_GLOBAL 0 (x)
6 LOAD_CONST 1 (600)
8 COMPARE_OP 8 (is)
10 RETURN_VALUE
In [9]: wat_wat_wat()
Out[9]: True
But this only goes further to prove my point - it's a bit of a silly question to quiz someone on :-) For one the question didn't seem to be well defined enough, but also it tests something that no one in their right mind would do, and also I don't know if the authors realized these nuances themselves either. I guess it makes for good trivia.
- squeaky-clean 9y agoHaha thanks, I was inspired by David Wolever's talk this year. This is the first time I've gone through the disassembler module or the CPython source, it's much less intimidating than I thought it would be. Also wat! The bytecode for the global case makes sense, but I wasn't expecting True?!
- makmanalp 9y agoYeah, now I'm even more confused!