python3 新特性
1、格式化字符串f-string
user = "Mike"
log_message = f'User{user} has logged in'
2、路径管理库Pathlib
3、类型提示Type hinting
def sentence_has_animal(sentence:str) -> bool:
return "animal" in sentence
4、枚举类enum
from enum import Enum, auto, unique
@unique #装饰器去重复
class monster(Enum):
zombie = auto()
warrior = auto()
bear = auto()
5、itertools模块
无限迭代器代码如下:
|
1
2
3
4
|
迭代器 参数 结果 例子count() start, [step] start, start+step, start+2*step, ... count(10) --> 10 11 12 13 14 ...cycle() p p0, p1, ... plast, p0, p1, ... cycle('ABCD') --> A B C D A B C D ...repeat() elem [,n] elem, elem, elem, ... endlessly or up to n times repeat(10, 3) --> 10 10 10 |
处理输入序列迭代器代码如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
迭代器 参数 结果 例子chain() p, q, ... p0, p1, ... plast, q0, q1, ... chain('ABC', 'DEF') --> A B C D E Fcompress() data, selectors (d[0] if s[0]), (d[1] if s[1]), ... compress('ABCDEF', [1,0,1,0,1,1]) --> A C E Fdropwhile() pred, seq seq[n], seq[n+1], starting when pred fails dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1groupby() iterable[, keyfunc] sub-iterators grouped by value of keyfunc(v)ifilter() pred, seq elements of seq where pred(elem) is True ifilter(lambda x: x%2, range(10)) --> 1 3 5 7 9ifilterfalse() pred, seq elements of seq where pred(elem) is False ifilterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8islice() seq, [start,] stop [, step] elements from seq[start:stop:step] islice('ABCDEFG', 2, None) --> C D E F Gimap() func, p, q, ... func(p0, q0), func(p1, q1), ... imap(pow, (2,3,10), (5,2,3)) --> 32 9 1000starmap() func, seq func(*seq[0]), func(*seq[1]), ... starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000tee() it, n it1, it2 , ... itn splits one iterator into ntakewhile() pred, seq seq[0], seq[1], until pred fails takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4izip() p, q, ... (p[0], q[0]), (p[1], q[1]), ... izip('ABCD', 'xy') --> Ax Byizip_longest() p, q, ... (p[0], q[0]), (p[1], q[1]), ... izip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D- |
组合生成器代码如下:
|
1
2
3
4
5
6
7
8
9
|
迭代器 参数 结果product() p, q, ... [repeat=1] cartesian product, equivalent to a nested for-looppermutations() p[, r] r-length tuples, all possible orderings, no repeated elementscombinations() p, r r-length tuples, in sorted order, no repeated elementscombinations_with_replacement() p, r r-length tuples, in sorted order, with repeated elementsproduct('ABCD', repeat=2) AA AB AC AD BA BB BC BD CA CB CC CD DA DB DC DDpermutations('ABCD', 2) AB AC AD BA BC BD CA CB CD DA DB DCcombinations('ABCD', 2) AB AC AD BC BD CDcombinations_with_replacement('ABCD', 2) AA AB AC AD BB BC BD CC CD DD |
6、LRU缓存,memoization技术
from functools import lru_cache
@lru_cache(maxsize=512)
def fib_memoization(number:int) -> int:
if number ==0 : return 0
if number ==1 : return 1
return fib_memoization(number - 1)
start = time.time()
fib_memoization(40)
print(f'Duration:{time.time() - start}s')
7、可扩展的可迭代对象解包
head, *bodey, tail = range(5) # 0, [1,2,3], 4
8、Data class装饰器,用来减少对样板代码的使用,该装饰器会自动生成__init()__和__repr()__方法。
class Armor:
def __init__(self, armor:float, description:str, level:int = 1):
self.armor = armor
self.level = level
self.description = description def power(self) -> float:
return self.armor * self.level armor = Armor(5.2, "common armor.", 2)
armor.power() #10.4 ###使用Data Class实现相同的Armor类
from dataclasses import dataclass
@dataclass
class Armor:
armor : float
description : str
level : int = 1 def power(self) -> float:
return self.armor *self.level armor = Armor(5.2, "common armor.", 2)
armor.power()
9、bisect模块保持列表排序:
这是一个免费的二分查找实现和快速插入有序序列的工具。也就是说,你可以使用:
import bisect
bisect.insort(list, element)
python3 新特性的更多相关文章
- Python3新特性 类型注解 以及 点点点
Python3新特性 类型注解 以及 点点点 ... Python3 的新特性 Python 是一种动态语言,变量以及函数的参数是 不区分类型 的 在 函数中使用类型注解 相当于 给 形参的 类型 设 ...
- Atitit python3.0 3.3 3.5 3.6 新特性 Python2.7新特性1Python 3_x 新特性1python3.4新特性1python3.5新特性1值得关注的新特性1Pyth
Atitit python3.0 3.3 3.5 3.6 新特性 Python2.7新特性1 Python 3_x 新特性1 python3.4新特性1 python3.5新特性1 值得关注的新特性1 ...
- Python3中的新特性(3)——代码迁移与2to3
1.将代码移植到Python2.6 建议任何要将代码移植到Python3的用户首先将代码移植到Python2.6.Python2.6不仅与Python2.5向后兼容,而且支持Python3中的部分新特 ...
- 相比于python2.6,python3.0的新特性。
这篇文章主要介绍了相比于python2.6,python3.0的新特性.更详细的介绍请参见python3.0的文档. Common Stumbling Blocks 本段简单的列出容易使人出错的变动. ...
- python3.8 新特性
https://docs.python.org/3.8/whatsnew/3.8.html python 3.8的新功能本文解释了与3.7相比,python 3.8中的新特性. 有关完整的详细信息,请 ...
- Python3.x 常用的新特性
Python3.x 常用的新特性 print() 是函数,不是一个语句 raw_input()输入函数,改为 input() Python 3 对文本和二进制数据做了更为清晰的区分. 文本由unico ...
- python-3.8.0 新特性之赋值表达式
[python-3.8.0 新特性之赋值表达式] 赋值表达式的语法是这样的“ name := expression ”,形式上看和赋值语句 “ = ” 差不多,就作用上来看也雷同.也就是说 “:=” ...
- Ubuntu安装Python3.8及新特性
Ubuntu安装Python3.8.0a4 如果你想体验一下,请用虚拟机(感受一下就行,别当真). 新特性(整体来说,有三点特别需要注意一下) 海象运算符 # python3.7 a = '123' ...
- 你应该知道的Python3.6、3.7、3.8新特性
很多人在学习了基本的Python语言知识后,就转入应用阶段了,后期很少对语言本身的新变化.新内容进行跟踪学习和知识更新,甚至连已经发布了好几年的Python3.6的新特性都缺乏了解. 本文列举了Pyt ...
随机推荐
- python相关软件安装流程图解——linux 安装python3——Python-3.7.1
请先确保已经安装了虚拟机 python相关软件安装流程图解——虚拟机安装——CentOS-7-x86_64-DVD-1810——CentOS-01下载 https://www.cnblogs.com/ ...
- react 组件的生命周期 超简版
组件从被创建到被销毁的过程称为组件的 生命周期: 通常,组件的生命周期可以被分为三个阶段:挂载阶段.更新阶段.卸载阶段: 一.挂载阶段 这个阶段组件被创建,执行初始化,并被挂载到DOM中,完成组件的第 ...
- (转)java源程序加密解决方案(基于Classloader解密)
转:http://cjnetwork.iteye.com/blog/851544 源程序加密解决方案 1. 概述: Java源程序的加密,有如下两种: 1使用混淆器对源码进行混淆,降低反编译工具的作用 ...
- Grunt入门
Grunt 新手一日入门 2014.06.20 前端相关 TOC 1. 用途和使用场景 2. 开发一个任务自动处理器 3. 开始学习 Grunt 3.1. 安装 Grunt 3.2. 生成 packa ...
- Appium测试过程中,建议使用谷歌输入法。用搜狗输入法报错报找不到元素,卡住
1. 手机使用谷歌输入法,在登录页面输入密码时输入数字时卡住报错 代码: 手机卡住 页面:看到页面上没有显示数字,所以卡住报错
- Eclipse 中安装 CDT 插件编写 C/C++
使用到的软件 1.Eclipse 开发工具 2.MinGW 编译器 一.Eclipse 中安装 CDT 插件 打开 Eclipse 插件市场 搜索 CDT,并找到如下的插件.插件的版本名字可能不太一样 ...
- python csv write 乱码
参考 : https://www.zhihu.com/question/34201726 1.使用utf_8_sig with open('d:/file.csv', 'w', encoding='u ...
- WPF 免费控件库
https://github.com/Infragistics/InfragisticsThemesForMicrosoftControls 几款WPF免费控件库,不过运行源码时需要下载三个DLL , ...
- Dubbo Ecosystem - 从微服务框架到微服务生态
从微服务框架到微服务生态,这是微服务发展的必然趋势,也是Dubbo社区满足开发者更高效的构建微服务体系期望的使命和担当. 近期,Apache Dubbo PPMC 望陶(社区昵称:ralf0131)做 ...
- 「题解」:[AHOI2013]作业
问题: 作业 时间限制: 10 Sec 内存限制: 512 MB 题面 题目描述 此时己是凌晨两点,刚刚做了Codeforces的小A掏出了英语试卷.英语作业其实不算多,一个小时刚好可以做完.然后是 ...