Re:Python-patterns – A collection of design patterns/idioms ...
by TekMol on Tuesday May 31, 2022 @07:52AM
(#31568483)
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?
[ Parent
| on HN ]
Re:Python-patterns – A collection of design patterns/idioms ...
by CraigJPerry on Tuesday May 31, 2022 @12:25PM
(#31571474)
Yeah, 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
Re:Python-patterns – A collection of design patterns/idioms ...
by TekMol on Tuesday May 31, 2022 @02:05PM
(#31572772)
Wow!
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!