函数即变量 #示范一:函数名称foo 加了()的意思是调用 def foo(): print('in the foo') #函数体 foo() 可以理解为:函数体 print('in the foo') 是一幢大楼的某个房间;foo就是房间的门牌号码。 变量同理,X=1 1是一幢大楼的某个房间;X就是房间的门牌号码。 #示范二: 两个函数都存在,正常运行 def bar(): print('in the bar') def foo(): print('in the foo') bar() foo() #示范三: 两个函数都存在,正常运行。 def foo(): print('in the foo') bar() def bar(): print('in the bar') foo() #示范四: 运行报错,调用foo函数时,bar函数还未加载 def foo(): print('in the foo') bar() foo() def bar(): print('in the bar')
有的函数可以不用起名字 比如lambda(匿名函数)
calc = lambda x:x*3