Skip to content

常用方法

查看类中包含的方法--dir

查看类中包含的方法

  dir(className)
  # 比如查看 unittest 类中的方法
  dir(unittest)
  # 查看 unittest.TestCase 子类包含的方法
  dir(unittest.TestCase)

查看某一个类/库的帮助文档--help

  # 查看库的帮助文档
  help(unittest)
    Help on class TestCase in module unittest.case:

  class TestCase(builtins.object)
  ……

  # 查看类的帮助文档
  help(unittest.TestCase)
  Help on package unittest:

  NAME
      unittest
  ……

判断字符串开头的字符

  • 方法一:str.startswith(str, beg=0,end=len(string)); str -- 检测的字符串。 strbeg -- 可选参数用于设置字符串检测的起始位置。 strend -- 可选参数用于设置字符串检测的结束位置。

示例: py str = "this is string example....wow!!!" print (str.startswith( 'this' )) True print (str.startswith( 'string', 8 )) True print (str.startswith( 'this', 2, 4 )) False

  • 方法二:token[0] == "str"

示例: py name = 'jackson' print(name[0] == 'j')

两者的区别,前者可以判断某一个字符串中是否已某个字符串开始;后者判断字符串中是否已某个字符开始。