dir 函数
在 Python 中,dir()
函数是一个内置函数,它用于列出对象的属性和方法。当你对一个对象调用 dir()
函数时,它会返回一个包含对象所有属性和方法名称的列表。
dir()
函数的基本用法
print(dir(object))
object
:可以是任何 Python 对象,包括模块、类、实例、函数等。
示例
假设我们有一个类 Person
和一个类的实例 a
:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
return f"Hello, my name is {
self.name} and I am {
self.age}<