当我们作为用户想了解一个 Class 的 Interface,即,它的 Attributes 和 Methods 的时候,常用的有三种方式:
- help(object)
- dir(object)
- object.dict
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
import datetime
class Golem:
def __init__(self, name=None):
self.name = name
self.built_year = datetime.date.today().year
def say_hi(self):
print('Hi!')
class runningGolem(Golem):
def run(self):
print('Can\'t you see? I\'m running...')
def say_hi(self): # 不再使用 Parent Class 中的定义,而是新的……
print('Hey! Nice day, Huh?')
rg = runningGolem('Clay')
help(rg)
dir(rg)
rg.__dict__
hasattr(rg, 'built_year')