18.07.17

一.函数名的运用

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

1.函数名可以赋值给其他变量

例:

def fn():    print("abc")print(fn)      #函数的内存地址a = fn         #将函数赋值给aa()            #调用函数==> <function fn at 0x00000229C37C2AE8>     abc

2.函数可以作为函数的参数

例:

def func(fn):    fn()def gn():    print("abc")func(gn)    ==>abc

3.函数名可以当做容器类的元素

例:

def fn1():    print("呵呵")def fn2():    print("嘿嘿")def fn3():    print("吼吼")lis = [fn1,fn2,fn3]for i in lis:    i()       ==>呵呵   嘿嘿   吼吼

4.函数名可以作为函数的返回值

例:

def gn():    def inner():        print("aaa")    return inner    #返回inner函数ret = gn()ret()    ==>aaa

二.闭包

1.闭包定义:内层函数对外层(非全局)的变量的引用.

变量写在全局是不安全.

例:

def fn():    name = "alex"    def inner():        print(name)    return innerret = fn()ret()   ==>alex    #访问inner函数

2.闭包的特点:

(1)安全(防止其它程序改变这个变量)

(2)常驻内存,提高效率.

3.查看闭包

__closure__()函数可以查看闭包.

如果不是闭包返回None 是闭包会返回cell

例:

def fn():    name = "alex"    def inner():        print(name)    inner()    print(inner.__closure__)fn()==> alex    (<cell at 0x00000204D86B9468: str object at 0x00000204D867D0D8>,)

三.迭代器

1.可迭代对象和迭代器

str.list.tuple.set.dict.range.文件句柄都是可迭代的数据类型

可迭代的数据类型中都有__iter__()函数.用dir()函数来查看一个数据类型中是否包含了那些函数.

例:

s = ‘abc‘print("__iter__"in dir(s))   ==>True

所有包含了__iter__函数的数据类型都是可迭代的数据类型

Iterable表示可迭代的

__iter__作用返回一个迭代器

__next__和__iter__两个函数都是迭代器的内部函数

例:

lst = ["abc","def","ghi"]it = lst.__iter__()print(it.__next__())    ==>abcprint(it.__next__())    ==>defprint(it.__next__())    ==>ghiprint(it.__next__())    ==>Error 迭代到最后一个之后,再迭代会报错

例:

lis = ["abc","def","ghi"]it = lis.__iter__()while 1:    try:        name = it.__next__()        print(name)    except StopIteration:        break==>abc  \n    def   \n   ghi

try...except固定搭配

例:

lst = [1,2,3]print("__iter__"in dir(lst))   ==>Trueprint("__next__"in dir(lst))   ==>False

例:

lst = [1,2,3]from collections import Iterablefrom collections import Iteratorprint(isinstance(lst,Iterable))   ==>Trueprint(isinstance(lst,Iterator))   ==>False

isinstance(对象,类型)判断对象是不是...类型

迭代器一定是可迭代的

文件句柄和range都是迭代器

2.迭代器的特点

(1)节省内存

(2)惰性机制

(3)不能反复,只能向下执行

原文地址:https://www.cnblogs.com/gxj742/p/9325558.html

时间: 2024-07-30 20:08:08

18.07.17的相关文章

2017.04.13-2017.07.17

QQ:577007217 今日更新: 2017.07.17 Geomagic Freeform 2017.0.93 Win64 1DVD Geomagic Freeform Plus 2017.0.93 Win64 1DVD Geomagic Sculpt 2017.0.93 Win64 1DVD InnovMetric.PolyWorks.2017.IR3.Win32_64 2DVD Mentor Graphics FloTHERM XT 3.1 Win64 1DVD PolyBoard Pr

张珺 2015/07/17 个人文档

姓名 张珺 日期 信息楼南406,中蓝公寓蓝芳园D507,2015/07/17 主要工作及心得 今天,我们先去找老师对程序进行了检查,并讨论了关于报告编写的问题.针对老师指出的问题,以及老师提出的意见.建议,我们在回来后对程序进行了修改.我主要负责对程序界面中中文显示及界面标题的问题进行修改.此外,我继续进行了报告的编写工作 遇到的问题 界面显示.提示信息中缺乏中文信息 解决方法 按照老师要求修改界面和对话框

HDU 1528 (二分图最大匹配 + 最小覆盖, 14.07.17)

Problem Description Adam and Eve play a card game using a regular deck of 52 cards. The rules are simple. The players sit on opposite sides of a table, facing each other. Each player gets k cards from the deck and, after looking at them, places the c

18.07.20(lambda().sorted().filter().map().递归.二分查找)

1.lambda() 匿名函数 lambda表示的是匿名函数,不需要用def来声明,一句话就可以声明一个函数 语法:函数名 = lambda 参数:返回值 注意: (1)参数可以有多个,多个参数用逗号隔开 (2)匿名函数不管多复杂.只写一行.且逻辑结束后直接返回数据 (3)返回值和正常函数一样1,可以是任意数据类型 我们通过__name__()查看函数名字 例: a = lambda x : x * xprint(a(5)) ==>25print(a.__name__) ==><lamb

18.9.17 poj2492 A Bug&#39;s Life

描述 BackgroundProfessor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of the opposite gender. In his experiment, individual bugs and their

18.12.17 POJ 1269 Intersecting Lines

描述 We all know that a pair of distinct points on a plane defines a line and that a pair of lines on a plane will intersect in one of three ways: 1) no intersection because they are parallel, 2) intersect in a line because they are on top of one anoth

18.07.01 luoguP1002 过河卒

题目描述 棋盘上 AA 点有一个过河卒,需要走到目标 BB 点.卒行走的规则:可以向下.或者向右.同时在棋盘上 CC 点有一个对方的马,该马所在的点和所有跳跃一步可达的点称为对方马的控制点.因此称之为“马拦过河卒”. 棋盘用坐标表示, AA 点 (0, 0)(0,0) . BB 点 (n, m)(n,m) ( nn , mm 为不超过 2020 的整数),同样马的位置坐标是需要给出的. 现在要求你计算出卒从 AA 点能够到达 BB 点的路径的条数,假设马的位置是固定不动的,并不是卒走一步马走一步

18.12.17 POJ 1569 Myacm Triangles

描述 There has been considerable archeological work on the ancient Myacm culture. Many artifacts have been found in what have been called power fields: a fairly small area, less than 100 meters square where there are from four to fifteen tall monuments

18.07.04

一.编码 1.ASCII  8位(bit) 1个字节  最多有256个位置,包含英文字母大小写.数字.标点等特殊符号. 2.国标码 GBK 16位(bit) 2个字节 包含大部分常用的汉字. 3.万国码 Unicode 32位(bit) 4个字节 4.utf-8 :    英文  8位(bit) 1个字节 欧洲文字  16bit  2个字节 中文   24bit 3个字节 8bit=1byte    1byte=1kb   1024kb=1mb   1024mb=1GB   1024GB=1TB