[Python]Understand attribute in Python

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()

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章