码迷,mamicode.com
首页 > 编程语言 > 详细

python 小兵(6)函数根据问题详解

时间:2019-01-14 14:32:01      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:操作   mac地址   通过   closure   end   append   函数   mac   print   

_list = []
for i in range(3):
  def func(a):
    return i+a
_list.append(func)
for f in _list:
  print(f(1))

 

首先操作_list.append(func)传入func的mac地址,然后打印列表出现的f是func的地址,然后通过print调用func函数,单数range已经运行完i=2所以答案等于3

 

_list = []
for i in range(3):
def func(i):
def f_closure(a): # <<<---
return i + a
return f_closure
_list.append(func(i)) # <<<---
for f in _list:
print(f(1))

这里面append添加的是函数所以f循环迭代的f也是地址"func(i)"但是是有值得mac地址.

python 小兵(6)函数根据问题详解

标签:操作   mac地址   通过   closure   end   append   函数   mac   print   

原文地址:https://www.cnblogs.com/huhuxixi/p/10265950.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!