**函数名的运用**

- [ ] 函数名是一个变量, 但它是一个特殊的变量, 与括号配合可以执行函数的变量

**函数名的内存地址**

```
  def func():
  print('666')
  print(func)
  ##结果<function func at 0x000001CAA4FCC1E0>
```
**函数名可以赋值给其他变量**

```
  def func():
  print('666')
  a = func #将func的函数名赋值给a
  a() #a() = func()

##结果 666
```
**函数名可以当作容器类的元素**

```
  def func1():
  print('666')
  def func2():
  print('555')
  def func3():
  print('444')
  def func4():
  print('333')
  func_list = [func1,func2,func3,func4]
  for item in func_list:
  item()

## 结果
  666
  555
  444
  333
```

**函数名可以当作函数的参数**

```
  def func():
  print("吃了么")
  def func2(fn):
  print("我是func2")
  fn() # 执行传递过来的fn
  print("我是func2")
  func2(func) # 把函数func当成参数传递给func2的参数fn.
```
**函数名可以作为函数的返回值**

```
  def func_1():
  print("这里是函数1")
  def func_2():
  print("这里是函数2")
  print("这里是函数1")
  return func_2
  fn = func_1()
  # 执行函数1. 函数1返回的是函数2, 这时fn指向的就是上面函数2
  fn() # 执行func_2函数
```
**闭包**

- [ ] 闭包就是内层函数,对外层函数(非全局)的变量的引用

```
  def func1():
  name = "张三"
  def func2():
  print(name)
  # 闭包
  func2()
  func1()
  # 结果: 张三
```
**检测闭包**

- [ ] 我们可以使用__closure__ 来检测函数是否是闭包. 使用函数名.__closure__返回cell就是
闭包. 返回None就不是闭包

```
def func1():
name = "张三"
def func2():
print(name)
func2()
print(func2.__closure__)
func1()
结果:
张三
(<cell at 0x0000020077EFC378: str object at 0x00000200674DC340>,)
```
**迭送器**

- [ ] 迭代操作对象有 str, list, tuple, dic, set。

```
  s = 'abc'
  for i in s:
  print(i)
  结果:
  a
  b
  c
错误的代码:
for i in 123:
print(i)
结果
Traceback (most recent call last):
File "D:/python_object/二分法.py", line 62, in <module>
for i in 123:
TypeError: 'int' object is not iterable
```
注意看报错信息,报错信息中有这样一句话: ‘int’ object is not iterable 翻译过来就是整数类型对象是不可迭代的.
iterable表示可迭代的.表示可迭代协议 那么如何进行验证你的数据类型是否符合可迭代协议.我们可以通过dir函数来查看类中定义好的所有方法

```
a = 'abc'
print(dir(a)) # dir查看对象的方法和函数
# 在打印结果中寻找__iter__ 如果存在就表示当前的这个类型是个可迭代对象
```
这是查看一个对象是否是可迭代对象的第一种方法,我们还可以通过isinstence()函数来查看一个对象是什么类型的

```
l = [1,2,3]
l_iter = l.__iter__()
from collections import Iterable
from collections import Iterator
print(isinstance(l,Iterable)) #True #查看是不是可迭代对象
print(isinstance(l,Iterator)) #False #查看是不是迭代器
print(isinstance(l_iter,Iterator)) #True
print(isinstance(l_iter,Iterable)) #True
```

通过上边的我们可以确定.如果对象中有__iter__函数,那么我们认为这个对象遵守了可迭代协议.就可以获取到相应的迭代器
**总结**
Iterable: 可迭代对象. 内部包含__iter__()函数

​ Iterator: 迭代器. 内部包含__iter__() 同时包含__next__().

迭代器的特点:

​ 1. 节省内存.

​ 2. 惰性机制

​ 3. 不能反复, 只能向下执行.

我们可以把要迭代的内容当成子弹. 然后呢. 获取到迭代器__iter__(), 就把子弹都装在弹夹中. 然后发射就是__next__()把每一个子弹(元素)打出来. 也就是说, for循环的时候.一开始的 时候是__iter__()来获取迭代器. 后面每次获取元素都是通过next()来完成的. 当程序遇到 StopIteration将结束循环.

python 基础学习笔记(7)--迭送器的更多相关文章

  1. 0003.5-20180422-自动化第四章-python基础学习笔记--脚本

    0003.5-20180422-自动化第四章-python基础学习笔记--脚本 1-shopping """ v = [ {"name": " ...

  2. python 基础学习笔记(1)

    声明:  本人是在校学生,自学python,也是刚刚开始学习,写博客纯属为了让自己整理知识点和关键内容,当然也希望可以通过我都博客来提醒一些零基础学习python的人们.若有什么不对,请大家及时指出, ...

  3. Python 基础学习笔记(超详细版)

    1.变量 python中变量很简单,不需要指定数据类型,直接使用等号定义就好.python变量里面存的是内存地址,也就是这个值存在内存里面的哪个地方,如果再把这个变量赋值给另一个变量,新的变量通过之前 ...

  4. Python基础学习笔记(十三)异常

    参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-exceptions.html Python用异常对象(excep ...

  5. Python基础学习笔记(十二)文件I/O

    参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-files-io.html ▶ 键盘输入 注意raw_input函 ...

  6. Python基础学习笔记(十一)函数、模块与包

    参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-functions.html 3. http://www.liao ...

  7. Python基础学习笔记(十)日期Calendar和时间Timer

    参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-date-time.html 3. http://www.liao ...

  8. Python基础学习笔记(九)常用数据类型转换函数

    参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-variable-types.html 3. http://www ...

  9. Python基础学习笔记(八)常用字典内置函数和方法

    参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-dictionary.html 3. http://www.lia ...

随机推荐

  1. CF600E Lomsat gelral (启发式合并)

    You are given a rooted tree with root in vertex 1. Each vertex is coloured in some colour. Let's cal ...

  2. BZOJ2809 dispatching(左偏树)

    在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依据自己的工作获取报偿.在这个帮派里,有一名忍者被称之为 Master.除了 Master以外,每名忍者都有且仅有一个上级.为保密,同时增强忍者们的 ...

  3. HDU-3339 IN ACTION(Dijkstra +01背包)

      Since 1945, when the first nuclear bomb was exploded by the Manhattan Project team in the US, the ...

  4. 笔记||Python3之算法

    循环嵌套:先从外层循环里面取出一个元素,再执行内层的循环:当内层的循环都执行后,再执行外层循环. for循环嵌套语法: for  x  in  range(): for  y  in  range() ...

  5. Spring MVC文件上传出现错误:Required MultipartFile parameter 'file' is not present

    1.配置文件上传的解析器 首先需要在spring mvc的配置文件中(注意是spring mvc的配置文件而不是spring的配置文件:applicationContext.xml)配置: sprin ...

  6. MySQL 数据查询小练习

    作业 # 创建班级表 create table class ( cls_id int auto_increment primary key, cls_name varchar(10) not null ...

  7. 轻松构建基于 Serverless 架构的弹性高可用音视频处理系统

    前言 随着计算机技术和 Internet 的日新月异,视频点播技术因其良好的人机交互性和流媒体传输技术倍受教育.娱乐等行业青睐,而在当前, 云计算平台厂商的产品线不断成熟完善, 如果想要搭建视频点播类 ...

  8. android studio 代码问题总结

    1,android studio隐藏title时,用eclipse里面的方法不行,所以用下面的代码解决,此代码需要写在 加载xml文件之后 getSupportActionBar().hide(); ...

  9. CCF-CSP题解 201604-4 游戏

    bfs #include <bits/stdc++.h> const int maxn = 100; using namespace std; int n, m, t; bool hasD ...

  10. 《Java基础知识》Java接口和抽象类的区别

    抽象类 抽象类必须用 abstract 修饰,子类必须实现抽象类中的抽象方法,如果有未实现的,那么子类也必须用 abstract 修饰.抽象类默认的权限修饰符为 public,可以定义为 public ...