Type of compiled regex object in python

后端 未结 10 1105
一个人的身影
一个人的身影 2020-11-30 07:14

What is the type of the compiled regular expression in python?

In particular, I want to evaluate

isinstance(re.compile(\'\'), ???)

10条回答
  •  北荒
    北荒 (楼主)
    2020-11-30 07:54

    When the type of something isn't well specified, there's nothing wrong with using the type builtin to discover the answer at runtime:

    >>> import re
    >>> retype = type(re.compile('hello, world'))
    >>> isinstance(re.compile('goodbye'), retype)
    True
    >>> isinstance(12, retype)
    False
    >>> 
    

    Discovering the type at runtime protects you from having to access private attributes and against future changes to the return type. There's nothing inelegant about using type here, though there may be something inelegant about wanting to know the type at all.

提交回复
热议问题