python给我嗯提供了四个关于反射的四个内置函数分别是:

  1. getattr
  2. setattr
  3. delattr
  4. hasattr
class Foo():
    _nav = "abc"

    def __init__(self, name, age):
        self.name = name
        self.age = age

    def work(self):
        print("工作!!!")


obj = Foo("小虎", "23")
print(obj.name)
print(obj.age)

obj.work()
# abc = obj.__dict__  # {'name': '小虎', 'age': '23'}
# abc = obj.__dir__()
abc = obj.__new__(object)
print(abc)

if hasattr(Foo, "_nav"):
    setattr(Foo, "_nav", "李杰")
    print(getattr(Foo,"_nav"))
else:
    print(22)