isinstance() necessary and helpful, sometimes

Jason Orendorff jason at jorendorff.com
Fri Jan 25 02:34:18 EST 2002


Raymond Hettinger wrote:
> 2.   In a similar vein, I needed to make sure that an iteration interface
> was supplied even when xrange or sequence types were supplied as 
> arguments:
>        gens = [ type(s)==types.GeneratorType and s or iter(s) for s in
> sequences]
> Was better replaced by:
>        gens = [ hasattr(s,'next') and s or iter(s) for s in sequences]


I think you'll find that "iter(s) is s" for iterator objects. So:
  gens = map(iter, sequences)


> In the following code, the goal is to accept a regular expression 
> pattern in the form of a string or in the form of an already
> compiled expression. [...]
>
> def tokenize(pattern, string=''):
>     if isinstance(pattern, str):
>         pattern = compile(pattern)
>     m = pattern.search(string, 0)
>     while m:
>         yield (m.lastgroup, m.group(m.lastgroup))
>         m = pattern.search(string, m.end())

I think hasattr(pattern, 'search') would have worked here.
If it has a search method, you presume it's a regex; a working
regex will return match objects that have lastgroup.

Anyway, by using isinstance(pattern, str) you're losing
unicode support.

How about...

def tokenize(pattern, string='', flags=0):
    if not hasattr(pattern, 'iterfind'):
        pattern = re.compile(pattern, flags)
    for m in pattern.iterfind(string):
        yield (m.lastgroup, m.group(m.lastgroup))

Interesting commentary overall; thanks.

## Jason Orendorff    http://www.jorendorff.com/




More information about the Python-list mailing list