@@ -234,8 +234,10 @@ code breakage.
234234
235235.. XXX talk about protocols?
236236
237- Note that for string operations Python has moved from external functions (the
238- ``string `` module) to methods. However, ``len() `` is still a function.
237+ .. note ::
238+
239+ For string operations, Python has moved from external functions (the
240+ ``string `` module) to methods. However, ``len() `` is still a function.
239241
240242
241243Why is join() a string method instead of a list or tuple method?
@@ -306,14 +308,15 @@ expensive. In versions of Python prior to 2.0 it was common to use this idiom::
306308This only made sense when you expected the dict to have the key almost all the
307309time. If that wasn't the case, you coded it like this::
308310
309- if dict.has_key (key):
311+ if key in dict(key):
310312 value = dict[key]
311313 else:
312314 dict[key] = getvalue(key)
313315 value = dict[key]
314316
315- (In Python 2.0 and higher, you can code this as ``value = dict.setdefault(key,
316- getvalue(key)) ``.)
317+ For this specific case, you could also use ``value = dict.setdefault(key,
318+ getvalue(key)) ``, but only if the ``getvalue() `` call is cheap enough because it
319+ is evaluated in all cases.
317320
318321
319322Why isn't there a switch or case statement in Python?
@@ -750,7 +753,7 @@ requested again. This is called "memoizing", and can be implemented like this::
750753
751754 # Callers will never provide a third parameter for this function.
752755 def expensive (arg1, arg2, _cache={}):
753- if _cache.has_key(( arg1, arg2)) :
756+ if ( arg1, arg2) in _cache :
754757 return _cache[(arg1, arg2)]
755758
756759 # Calculate the value
0 commit comments