[Python-Dev] type categories -- an example

Brett Cannon drifty@bigfoot.com
Mon, 19 Aug 2002 15:49:44 -0700 (PDT)


[Jeremy Hylton]

> >>>>> "AK" == Andrew Koenig <ark@research.att.com> writes:
>
>   Jeremy> I'd put this at the module level: compiled_re_type =
>   Jeremy> type(re.compile(""))
>
>   Jeremy> Then you can use isistance() to test:
>
>   Jeremy> isinstance(re.compile("spam+"), compiled_re_type)
>
>   AK> But is it guaranteed that re.compile will always yield an object
>   AK> of the same type?
>
> Hard to say.  I can read the code and see that the current
> implementation will always return objects of the same type.  In fact,
> it's using type(sre_compile.compile("", 0)) internally to represent
> that type.
>
> That's not a guarantee.
<snip>

This might be a stupid question, but why wouldn't
isinstance(re.compile("spam+"), type(re.compile(''))) always work (this is
Jeremey's code, just inlined)?  Unless the instance being tested was
marshalled (I think), the test should always work.  Even using an
unpickled instance (I think, again) should work since it would use the
current implementation of a pattern object.  So as long as the instance
being tested is not somehow being stored and then brought back using a
newer version of Python it should always work.

If not true, then I have been lied to. =)

-Brett C.