OO conventions
Alex Martelli
aleaxit at yahoo.com
Sat Feb 4 10:40:21 EST 2006
Blair P. Houghton <blair.houghton at gmail.com> wrote:
...
> > It is (search for 'staticmethod' and 'classmethod'). But there's not
> > much use for 'static methods' in Python - we usually just use plain
> > functions ('classmethods' are another beast - much more useful than
> > staticmethods)
>
> Does it make any noticeable difference in efficiency, or does nobody
> care much about efficiency in Python?
Some of us do, at few but crucial moments; that's why we have timeit.py
to let you measure the performance of pieces of code you care about.
helen:~ alex$ python -mtimeit -s'class X(object):
> @staticmethod
> def f(): pass
> x=X()' 'x.f()'
1000000 loops, best of 3: 1.07 usec per loop
helen:~ alex$ python -mtimeit -s'def f(): pass' 'f()'
1000000 loops, best of 3: 0.727 usec per loop
helen:~ alex$
As you see, static methods have a small extra lookup cost (a couple
hundred nanoseconds on my oldish laptop); normally, one would use a
function instead. But if the body of the function/method did some
significant, the lookup overhead would then matter little; clarity and
organization and simplicity, OTOH, _always_ matter. "Premature
optimization is the root of all evil in programming", as Knuth wrote
quoting Hoare -- and anybody who's read Knuth's work knows he is
anything BUT indifferent to real optimization; the key is avoiding that
"premature" part!-)
Alex
More information about the Python-list
mailing list