[Python-Dev] type categories -- an example

Andrew Koenig ark@research.att.com
Tue, 20 Aug 2002 10:19:35 -0400 (EDT)


>> But is it guaranteed that re.compile will always yield
>> an object of the same type?

Guido> There are no guarantees in life, but I expect that that is something
Guido> that plenty of code depends on, so it will likely stay that way.

The kind of situation I imagine is that a regular expression might be
implemented not just as a single type but as a whole hierarchy of
them, with the particular type used for a regular expression depending
on thevalue of the regular expression.  For example:

   class Compiled_regexp(object):
      # ...

   class Anchored_regexp(Compiled_regexp):
      # ...

   class Unanchored_regexp(Compiled_regexp):
      # ...

where whether a regexp is anchored or unanchored depends on whether it
begins with "^".  (Contrived, but you get the idea).  In that case, it
is entirely possible that re.compile("") and re.compile("^foo") return
types such that neither is an instance of the other.

I understand that the regexp library doesn't work this way, and will
probably never work this way, but I'm using this example to show why
the technique of using the type returned by a particular library function
call to identify the results of future calls doesn't work in general.