首页 > 代码库 > [Python]Understand attribute in Python

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

 

[Python]Understand attribute in Python