Hacker News new | past | comments | ask | show | jobs | submit login

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?



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


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!




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: