class CLanguage: def __init__ (self,): self.name = "C语言中文网" self.add = "http://task.lmcjl.com" def say(): pass clangs = CLanguage() print(dir(clangs))程序运行结果为:
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'add', 'name', 'say']
值得一提的是,dir() 函数的内部实现,其实是在调用参数对象 __dir__() 方法的基础上,对该方法返回的属性名和方法名做了排序。class CLanguage: def __init__ (self,): self.name = "C语言中文网" self.add = "http://task.lmcjl.com" def say(): pass clangs = CLanguage() print(clangs.__dir__())程序运行结果为:
['name', 'add', '__module__', '__init__', 'say', '__dict__', '__weakref__', '__doc__', '__repr__', '__hash__', '__str__', '__getattribute__', '__setattr__', '__delattr__', '__lt__', '__le__', '__eq__', '__ne__', '__gt__', '__ge__', '__new__', '__reduce_ex__', '__reduce__', '__subclasshook__', '__init_subclass__', '__format__', '__sizeof__', '__dir__', '__class__']
显然,使用 __dir__() 方法和 dir() 函数输出的数据是相同,仅仅顺序不同。
本文链接:http://task.lmcjl.com/news/4200.html