[Python-ideas] Mitigating 'self.' Method Pollution

Sven R. Kunze srkunze at mail.de
Fri Jul 24 16:22:48 CEST 2015


My stance on this: that's the responsibility of the Python interpreter 
not the one of the Python programmer.

Reminds me of the string concatenation issue of older Python versions:

  - 
https://wiki.python.org/moin/PythonSpeed/PerformanceTips#String_Concatenation
  - 
https://wiki.python.org/moin/PythonSpeed/PerformanceTips#Avoiding_dots...



On 12.07.2015 17:11, Anders J. Munch wrote:
> Terry Reedy wrote:
> > Localization, which is not limited to self attributes, does two
> > things: it removes the need to continually type 'object.x'; it makes
> > the remaining code run faster.  I would be interested to know how
> > many faster local references are needed to make up for the overhead
> > of localization.
>
> If you access the attribute just twice, that's enough for localisation 
> to yield
> a speedup.  Local variable access is just that much faster than 
> attribute lookup.
>
> $ python3 -V
> Python 3.4.3Terry Reedy wrote:
> > Localization, which is not limited to self attributes, does two 
> things: it
> > removes the need to continually type 'object.x'; it makes the 
> remaining code
> > run faster.  I would be interested to know how many faster local 
> references
> > are needed to make up for the overhead of localization.
>
> If you access the attribute just twice, that's enough for localisation 
> to yield
> a speedup.  Local variable access is just that much faster than 
> attribute lookup.
>
> $ python3 -V
> Python 3.4.3
> $ python3 -m timeit -s "class C: pass" -s "c = C();c.x=1" "c.x;c.x"
> 10000000 loops, best of 3: 0.106 usec per loop
> $ python3 -m timeit -s "class C: pass" -s "c = C();c.x=1" "x=c.x;x;x"
> 10000000 loops, best of 3: 0.0702 usec per loop
>
> $ python -V
> Python 2.7.3
> $ python -m timeit -s "class C(object): pass" -s "c = C();c.x=1" 
> "c.x;c.x"
> 10000000 loops, best of 3: 0.105 usec per loop
> $ python -m timeit -s "class C(object): pass" -s "c = C();c.x=1" 
> "x=c.x;x;x"
> 10000000 loops, best of 3: 0.082 usec per loop
>
> regards, Anders
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/



More information about the Python-ideas mailing list