Python dir()函数

在Python中,dir()函数是一个内置函数,用于列出指定对象的属性和方法。如果没有指定对象,则默认使用当前作用域中的所有对象。dir()函数返回一个字符串列表,包含指定对象的所有属性和方法名称。

dir()函数示例

以下是一些dir()函数的示例:

  • 列出当前作用域中的所有对象的名称:
  • print(dir())

    运行效果:

    ['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']
  • 列出一个模块的所有属性和方法:
  • import math
    print(dir(math))

    运行效果:

    ['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'lcm', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'nextafter', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc', 'ulp']
  • 列出一个对象的所有方法:
  • s = "hello, world"
    print(dir(s))

    运行效果:

    ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'removeprefix', 'removesuffix', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
  • 定义了__dir__()方法的类:
  • class Test:
        def __dir__(self):
            return ['a','b','c']
    
    t = Test()
    print(dir(t))

    运行效果:

    ['a', 'b', 'c']

需要注意的是,dir()函数返回的列表中还包括一些双下划线开头和结尾的名称,这些名称通常是Python中的特殊方法或属性。例如,__len__()方法用于获取对象的长度,__str__()方法用于将对象转换为字符串等。这些特殊方法和属性在Python中具有特殊的含义,可以用来实现自定义的行为和功能。

原创内容,如需转载,请注明出处;

本文地址: https://www.perfcode.com/python-built-in-functions/python-dir.html

分类: 计算机技术
推荐阅读:
Windows安装Visual Studio Code Visual Studio Code 是一个运行于Mac OS、Windows、Linux之上的,针对于编写现代Web 和运用的跨平台源代码编辑器;
解决Golang中cannot refer to unexported name xxxx这类错误 在Golang中,碰到cannot refer to unexported name xxxx,这类错误,通常是你调用了一个包内不存在的函数导致的;
MySQL AES_ENCRYPT()、AES_DECRYPT()函数 AES_ENCRYPT()和AES_DECRYPT()使用官方AES算法实现数据的加密和解密;其密钥默认长度为128位,默认块加密模式为ECB;
PySide6 使用QIcon为按钮添加图标 在PySide6中为按钮添加图标,可以使用QIcon类来加载图标文件,并使用QPushButton类的setIcon()方法将图标设置给按钮;
SEO基础:TKD是什么意思? TKD其实就是HTML语言中title、keywords、description的缩写;
打开任务管理的一瞬间CPU飙到很高,这种现象正常吗? Windows系统上的任务管理器(进程名Taskmgr.exe)用于实时显示计算机当前有关于性能、进程、服务等信息;细心的朋友会发现,在打开任务管理器的一瞬间,CPU使用率瞬间飙升,基本上会达到100%,随后也会很快回落,那这种现象正常么?