3 ms·
If I understand you correctly, only the creation of greet1 and greet2 are slower, but then the usage of these objects is just as fast as using instances of clas
by TekMol 4y ago
If I understand you correctly, only the creation of greet1 and greet2 are slower, but then the usage of these objects is just as fast as using instances of classes?
- CraigJPerry 4y agoYeah, I was pretty sure that was the case but just to be certain: >>> from dis import dis >>> dis(greet) 4 0 LOAD_GLOBAL 0 (print) 2 LOAD_CONST 1 ('Hello ') 4 LOAD_GLOBAL 1 (name) 6 BINARY_ADD 8 CALL_FUNCTION 1 10 POP_TOP 12 LOAD_CONST 0 (None) 14 RETURN_VALUE >>> dis(greet1.greet) 4 0 LOAD_GLOBAL 0 (print) 2 LOAD_CONST 1 ('Hello ') 4 LOAD_GLOBAL 1 (name) 6 BINARY_ADD 8 CALL_FUNCTION 1 10 POP_TOP 12 LOAD_CONST 0 (None) 14 RETURN_VALUE
- TekMol 4y agoWow! So with this we can skip the class definition, having "self" as the first parameter of every function and prefixing all class variables with "self". That leads to so much cleaner code!