Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在if语句中使用类型([object])时,为什么必须将其类型转换为字符串?_Python_Python 3.x_Variables - Fatal编程技术网

Python 在if语句中使用类型([object])时,为什么必须将其类型转换为字符串?

Python 在if语句中使用类型([object])时,为什么必须将其类型转换为字符串?,python,python-3.x,variables,Python,Python 3.x,Variables,当我执行以下代码时: import re phoneNumRegex = re.compile("") print(type(phoneNumRegex)) 我得到输出: <class '_sre.SRE_Pattern'> 注意上面的str(type(phoneNumRegex)),而不是type(phoneNumRegex)。我的问题是,当我在if语句中使用type(phoneNumRegex)时,为什么必须将其键入到str中?这是因为返回类型对象: >>类型(类型(phon

当我执行以下代码时:

import re
phoneNumRegex = re.compile("")
print(type(phoneNumRegex))
我得到输出:

<class '_sre.SRE_Pattern'>
注意上面的
str(type(phoneNumRegex))
,而不是
type(phoneNumRegex)
。我的问题是,当我在if语句中使用
type(phoneNumRegex)
时,为什么必须将其键入到
str
中?

这是因为返回类型对象:

>>类型(类型(phoneNumRegex))
检查变量是否属于某一类型的更好方法是使用。对于正则表达式模式类型:

    • 这是因为返回类型对象:

      >>类型(类型(phoneNumRegex))
      
      检查变量是否属于某一类型的更好方法是使用。对于正则表达式模式类型:

      因为
      type()
      不返回字符串,所以它返回一个
      type

      >>> a = 3
      >>> type(a)
      <type 'int'>
      >>> type(type(a))
      <type 'type'>
      
      >a=3
      >>>类型(a)
      >>>类型(类型(a))
      
      当您打印多种类型的对象时,它将自动调用
      \uuuu str\uuu()
      ,以获取要打印的字符串。在条件中使用它时,必须手动执行此操作。

      因为
      type()
      不返回字符串,它返回
      类型

      >>> a = 3
      >>> type(a)
      <type 'int'>
      >>> type(type(a))
      <type 'type'>
      
      >a=3
      >>>类型(a)
      >>>类型(类型(a))
      

      当您打印多种类型的对象时,它将自动调用
      \uuuu str\uuu()
      ,以获取要打印的字符串。在条件中使用它时,必须手动执行此操作。

      在这种特殊情况下,可以通过这种方式比较类型

      import re
      phoneNumRegex = re.compile("")
      if phoneNumRegex.__class__  == re._pattern_type:
          print("This if statemnt works")
      

      在这种特殊情况下,可以通过这种方式比较类型

      import re
      phoneNumRegex = re.compile("")
      if phoneNumRegex.__class__  == re._pattern_type:
          print("This if statemnt works")
      

      您正在与字符串进行比较,因此当然需要与字符串进行比较。与类型进行比较:

      >>> import re
      >>> re_pattern_type = type(re.compile(''))
      >>> re_pattern_type
      <class '_sre.SRE_Pattern'>
      >>> phone_regex = re.compile('\d{3}-\d{3}-\d{4}')
      >>> phone_regex
      <_sre.SRE_Pattern object at 0x0000000003F6FD50>
      >>> if type(phone_regex) == re_pattern_type:
      ...     print('match')
      ... 
      match
      

      您正在与字符串进行比较,因此当然需要与字符串进行比较。与类型进行比较:

      >>> import re
      >>> re_pattern_type = type(re.compile(''))
      >>> re_pattern_type
      <class '_sre.SRE_Pattern'>
      >>> phone_regex = re.compile('\d{3}-\d{3}-\d{4}')
      >>> phone_regex
      <_sre.SRE_Pattern object at 0x0000000003F6FD50>
      >>> if type(phone_regex) == re_pattern_type:
      ...     print('match')
      ... 
      match
      

      我认为你把“解析”和“类型转换”混淆了:@Signal你是对的我接受了你的编辑我认为你把“解析”和“类型转换”混淆了:@Signal你是对的我接受了你的编辑