[Python-Dev] Draft Guide for code migration and modernation

Walter Dörwald walter@livinglogic.de
Tue, 04 Jun 2002 13:58:53 +0200


Neal Norwitz wrote:

> [...]
> Pattern:  import string ; string.method(s, ...)  -->  s.method(...)

join and zfill should probably be mentioned separately:
"""
Be careful with string.join(): The order of the arguments is reversed
here.

string.zfill has a "decadent feature": It also works for
non-string objects by calling repr before formatting.
"""

             string.atoi(s, ...)  -->  int(s, ...)
             string.atol(s, ...)  -->  long(s, ...)
             string.atof(s, ...)  -->  float(s, ...)

>           c in string.whitespace --> c.isspace()

This changes the meaning slightly for unicode characters, because
chr(i).isspace() != unichr(i).isspace()
for i in { 0x1c, 0x1d, 0x1e, 0x1f, 0x85, 0xa0 }

New ones:

Pattern:  "foobar"[:3] == "foo" -> "foobar".startswith("foo")
           "foobar"[-3:] == "bar" -> "foobar".endswith("bar")
Version:  ??? (It was added on the string_methods branch)
Benefits: Faster because no slice has to be created.
           No danger of miscounting.
Locating: grep "\[\w*-[0-9]*\w*:\w*\]" | grep "=="
           grep "\[\w*:\w*[0-9]*\w*\]" | grep "=="

Pattern:  import types;
           if hasattr(types, "UnicodeType"):
               foo
           else:
               bar
           -->
           try:
               unicode
           except NameError:
               bar
           else:
               foo
Idea:     The types module will likely to be deprecated in the future.
Version:  2.2
Benefits: Avoid a deprecated feature.
Locating: grep "hasattr.*UnicodeType"

Bye,
    Walter Dörwald