@@ -234,8 +234,10 @@ code breakage.
234
234
235
235
.. XXX talk about protocols?
236
236
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.
239
241
240
242
241
243
Why 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::
306
308
This only made sense when you expected the dict to have the key almost all the
307
309
time. If that wasn't the case, you coded it like this::
308
310
309
- if dict.has_key (key):
311
+ if key in dict(key):
310
312
value = dict[key]
311
313
else:
312
314
dict[key] = getvalue(key)
313
315
value = dict[key]
314
316
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.
317
320
318
321
319
322
Why 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::
750
753
751
754
# Callers will never provide a third parameter for this function.
752
755
def expensive (arg1, arg2, _cache={}):
753
- if _cache.has_key(( arg1, arg2)) :
756
+ if ( arg1, arg2) in _cache :
754
757
return _cache[(arg1, arg2)]
755
758
756
759
# Calculate the value
0 commit comments