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()
本文通过一个简单的Python代码示例展示了如何区分类的属性和项。定义了一个名为MyClass的类,并通过__getitem__方法实现了对类实例的字典式访问。通过setattr设置了类实例的属性,展示了属性和项的不同调用方式。
5120

被折叠的 条评论
为什么被折叠?



