Some syntactic sugar proposals
Tim Chase
python.list at tim.thechases.com
Mon Nov 15 07:46:59 EST 2010
On 11/15/2010 12:39 AM, Dmitry Groshev wrote:
> x in range optimisation
I've often thought this would make a nice O(1)-test lookup on an
xrange() generator. I don't have strong use-cases for it, but a
bit of well-tested code in the standard library would save me
from doing the math (along with its potential fenceposting and
sign errors) the couple times I've reached for it. Using the
x in list(xrange(...)) # or "range()" depending your version
ends up with an O(n) lookup, though I suppose one could create a
set() for O(1) lookups, but that still requires O(N) storage
(where a mathematical lookup would involve O(1) storage *and* time)
> foo() if foo() else bar()
This is usually indicative that you need to cache the
object...something like
etcc = expensive_to_calculate_constructor()
result = foo(etcc) if test(etcc) else bar()
That way, you cache the potentially-expensive calculation,
indicate to other readers-of-your-code that it's
potentially-expensive, and enforce the use of that cache to
ensure that you don't duplicate the expensive op.
-tkc
More information about the Python-list
mailing list