This recipe is a small code snippet of showing how to distinguish attribute and item in Python language.
class MyClass(object): def __getitem__(self, name): return "this is from get item function" pass def action(): return "play basketball this afternoon" if __name__ == "__main__": me = MyClass() setattr(me,"weather","it is sunshine") setattr(me,"event",action) print me.weather print me['event'] print me.event()
时间: 2024-10-09 20:43:07