# from enum import Enum
# 枚举
# class VIP(Enum):
# YELLOW =
# YELLOW_ALIAS = # 别名
# GREEN =
# BLACK =
# RED = # print(VIP.GREEN) # VIP.GREEN8VIP.GREEN
# print(VIP.GREEN.name) # GREEN
# print(VIP['GREEN']) # VIP.GREEN # for v in VIP:
# print(v)
# VIP.YELLOW
# VIP.GREEN
# VIP.BLACK
# VIP.RED # for v in VIP.__members__.items():
# print(v)
# ('YELLOW', <VIP.YELLOW: >)
# ('YELLOW_ALIAS', <VIP.YELLOW: >)
# ('GREEN', <VIP.GREEN: >)
# ('BLACK', <VIP.BLACK: >)
# ('RED', <VIP.RED: >) # for v in VIP.__members__:
# print(v)
# YELLOW
# YELLOW_ALIAS
# GREEN
# BLACK
# RED # result = VIP.GREEN == VIP.BLACK
# print(result) # False
# result = VIP.GREEN ==
# print(result) # False
# result =VIP.GREEN is VIP.GREEN
# print(result) # True # a =
# print(VIP(a)) # VIP.YELLOW # from enum import IntEnum
# class VIP(IntEnum):
# YELLOW =
# GREEN =
# BLACK = 'str'
# RED = from enum import IntEnum,unique
@unique
class VIP(IntEnum):
YELLOW =
GREEN =
BLACK =
RED =
# 闭包=函数 + 环境变量
# def curve_pre():
# a = # 环境变量
# def curve(x):
# # print('this is a function')
# return a*x*x
# return curve # f = curve_pre()
# print(f()) # # b =
# def f1(x):
# return b * x * x
# print(f1()) # # a =
# f = curve_pre()
# print(f()) #
# print(f.__closure__) # (<cell at 0x00F1C2D0: int object at 0x65DDE490>,)
# print(f.__closure__[].cell_contents) # # c =
# def curve_pre():
# def curve(x):
# # print('this is a function')
# return c*x*x
# return curve
# c =
# f = curve_pre()
# print(f()) # # def f1():
# a =
# def f2():
# a =
# print(a) #.
# print(a) #.
# f2()
# print(a) #. # f1() # def f1():
# a =
# def f2():
# a =
# return a
# return f2 # f = f1()
# print(f) # <function f1.<locals>.f2 at 0x02AC5198>
# print(f.__closure__) # None # def f1():
# a =
# def f2():
# return a
# return f2 # f = f1()
# print(f) # <function f1.<locals>.f2 at 0x03995198>
# print(f.__closure__) # (<cell at 0x01E0C270: int object at 0x65DDE3A0>,) # origin =
# def go(step):
# new_pos = origin + step
# # origin = new_pos
# return origin # print(go()) #
# print(go()) #
# print(go()) # # 非闭包的实现
# origin =
# def go(step):
# global origin
# new_pos = origin + step
# origin = new_pos
# return origin # print(go()) #
# print(go()) #
# print(go()) # # 闭包的实现
origin =
def factory(pos):
def go(step):
nonlocal pos
new_pos = pos + step
pos = new_pos
return new_pos
return go tourist = factory(origin)
print(tourist()) #
print(tourist()) #
print(tourist()) #

12.Python的高级语法和用法的更多相关文章

  1. Python(九) Python的高级语法与用法

    本章节我们揭开Python进阶部分的高级特性,详细讲解枚举.闭包,并对函数式编程做出介绍 一. 枚举其实是一个类 from enum import Enum #枚举类 class VIP(Enum): ...

  2. 十一、python的高级语法与用法

    一.枚举其实是一个类 现实世界中的“类型”,在计算机世界中如何描述? 常见的 1)用1.2.3..等数字表示类型 2)较好的做法是用字典表示 3)最好的是使用枚举 # coding=utf-8 fro ...

  3. python 类高级语法 静态方法

    通过@staticmethod装饰器即可把其装饰的方法变为一个静态方法,什么是静态方法呢?其实不难理解,普通的方法,可以在实例化后直接调用,并且在方法里可以通过self.调用实例变量或类变量,但静态方 ...

  4. Python自动化 【第七篇】:Python基础-面向对象高级语法、异常处理、Scoket开发基础

    本节内容: 1.     面向对象高级语法部分 1.1   静态方法.类方法.属性方法 1.2   类的特殊方法 1.3   反射 2.     异常处理 3.     Socket开发基础 1.   ...

  5. Python高级语法之:一篇文章了解yield与Generator生成器

    Python高级语法中,由一个yield关键词生成的generator生成器,是精髓中的精髓.它虽然比装饰器.魔法方法更难懂,但是它强大到我们难以想象的地步:小到简单的for loop循环,大到代替多 ...

  6. python 循环高级用法 [expression for x in X [if condition] for y in Y [if condition] ... for n in N [if condition] ]按照从左至右的顺序,分别是外层循环到内层循环

    高级语法 除了像上面介绍的 [x ** 2 for x in L] 这种基本语法之外,列表推导式还有一些高级的扩展. 4.1. 带有if语句 我们可以在 for 语句后面跟上一个 if 判断语句,用于 ...

  7. 第4天 | 12天搞定Python,基础语法(下)

    为了方便你的学习,减轻负重,我特意将基础语法分成上下两部分.希望你喜欢这种方式,如果不喜欢,你可以跟我说,反正我是不会改的,哈哈~~. 如果上部分,你还没看的话,先去看<第4天 | 12天搞定P ...

  8. 进击的Python【第五章】:Python的高级应用(二)常用模块

    Python的高级应用(二)常用模块学习 本章学习要点: Python模块的定义 time &datetime模块 random模块 os模块 sys模块 shutil模块 ConfigPar ...

  9. Python正则式的基本用法

    Python正则式的基本用法 1.1基本规则 1.2重复 1.2.1最小匹配与精确匹配 1.3前向界定与后向界定 1.4组的基本知识 2.re模块的基本函数 2.1使用compile加速 2.2 ma ...

随机推荐

  1. 「题解」JOIOI 王国

    「题解」JOIOI 王国 题目描述 考场思考 正解 题目描述 点这里 考场思考 因为时间不太够了,直接一上来就着手暴力.但是本人太菜,居然暴力爆 000 ,然后当场自闭- 一气之下,发现对 60pts ...

  2. Java中Comparator的使用

    在某些特殊情况,我们需要对一个对象数组或集合依照对应的属性排序:此时,我们就可以用Comparator接口处理. 上代码 TestComparaTo 类 package com.test.interf ...

  3. postman提交文件

    说明 1.Headers中添加 Content-Type      multipart/form-data 2.Body 中选择form-data 并添加 需要传的参数名和值 最后新的一行选择file ...

  4. Mp3下载

  5. C#用SQLDMO操作数据库----转载

    C#用SQLDMO操作数据库 sqldmo.dll是随sql server2000一起发布的.sqldmo.dll自身是一个com对象 sqldmo(sql distributed managemen ...

  6. 【android官方文档】与其他App交互

    发送用户到另外一个App YOU SHOULD ALSO READ 内容分享 One of Android's most important features is an app's ability ...

  7. java中栈内存与堆内存(JVM内存模型)

    java中栈内存与堆内存(JVM内存模型) Java中堆内存和栈内存详解1 和 Java中堆内存和栈内存详解2 都粗略讲解了栈内存和堆内存的区别,以及代码中哪些变量存储在堆中.哪些存储在栈中.内存中的 ...

  8. 学习angularJs(1)--引用文件

    <script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js">< ...

  9. windos常见软件库

    1.护卫神软件库 http://soft.huweishen.com/special/1.html 2.护卫神windows资料库 http://v.huweishen.com/ 3.国超软件下载 h ...

  10. Myeclipse项目出现红叉解决方案

    1.右键点击你的项目.选中properties 2.选中MyEclipse下的Project Facets里面的java 此时的版本号为1.5,修改 3.选中MyEclipse下的Project Fa ...