赞
踩
Python3.12之前模块忘记导入是这样的:
>>> sys.version_info
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'sys' is not defined
新版本会提醒你是否忘记导入模块:
>>> sys.version_info
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'sys' is not defined. Did you forget to import 'sys'?
忘记在类中写self也会提醒你:
class A:
def __init__(self):
self.blech = 1
def foo(self):
somethin = blech
>>> A().foo()
Traceback (most recent call last):
File "<stdin>", line 1
somethin = blech
^^^^^
NameError: name 'blech' is not defined. Did you mean: 'self.blech'?
from…import…语句写反也会提醒你:
import a.y.z from b.y.z
Traceback (most recent call last):
File "<stdin>", line 1
import a.y.z from b.y.z
^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Did you mean to use 'from ... import ...' instead?
导入模块时,稍稍写错了模块名也会提醒你:
from collections import chainmap
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name 'chainmap' from 'collections'. Did you mean: 'ChainMap'?
f"{}"
中可以使用表达式了~
>>> songs = ['Take me back to Eden', 'Alkaline', 'Ascensionism']
>>> f"This is the playlist: {", ".join(songs)}"
'This is the playlist: Take me back to Eden, Alkaline, Ascensionism'
f"{}"
可以嵌套了~
f"""{f'''{f'{f"{1+1}"}'}'''}"""
'2'
多行表达式和注释:
>>> f"This is the playlist: {", ".join([
... 'Take me back to Eden', # My, my, those eyes like fire
... 'Alkaline', # Not acid nor alkaline
... 'Ascensionism' # Take to the broken skies at last
... ])}"
'This is the playlist: Take me back to Eden, Alkaline, Ascensionism'```
反斜杠和unicode
字符!!
这个真的很赞!以前要是f-string表达式中出现
\
字符,这个表达式基本就废了,现在靠反斜杠转下就行了!
print(f"This is the playlist: {"\n".join(songs)}")
This is the playlist: Take me back to Eden
Alkaline
Ascensionism
print(f"This is the playlist: {"\N{BLACK HEART SUIT}".join(songs)}")
This is the playlist: Take me back to Eden♥Alkaline♥Ascensionism
@override
在Java中经常被使用,它的用途是当子类的方法想覆盖父类方法的默认实现时。
通过@override
装饰器来表示,用来告诉编译器,这是一个被重写的方法,当重写的子方法被调用时,不需再去调父类的方法了。
from typing import override
class Base:
def get_color(self) -> str:
return "blue"
class GoodChild(Base):
@override # ok: overrides Base.get_color
def get_color(self) -> str:
return "yellow"
class BadChild(Base):
@override # type checker error: does not override Base.get_color
def get_colour(self) -> str:
return "red"
Python一直向Java靠拢,Java一直向Python学习。
据说Python3.12还更新了子解释器这个概念Sub-Interpreters
,这难道离突破GIL的封锁又近了一步?
有兴趣的小伙伴可以通过以下传送门学习研究下Sub-Interpreters
的使用,本文就不赘述了。
>>> 传送门:Real Multithreading is Coming to Python - Learn How You Can Use It Now <<<
读者福利:如果大家对Python感兴趣,这套python学习资料一定对你有用
对于0基础小白入门:
如果你是零基础小白,想快速入门Python是可以考虑的。
一方面是学习时间相对较短,学习内容更全面更集中。
二方面是可以根据这些资料规划好学习计划和方向。
包括:Python激活码+安装包、Python web开发,Python爬虫,Python数据分析,人工智能、机器学习等习教程。带你从零基础系统性的学好Python!
① Python所有方向的学习路线图,清楚各个方向要学什么东西
② 600多节Python课程视频,涵盖必备基础、爬虫和数据分析
③ 100多个Python实战案例,含50个超大型项目详解,学习不再是只会理论
④ 20款主流手游迫解 爬虫手游逆行迫解教程包
⑤ 爬虫与反爬虫攻防教程包,含15个大型网站迫解
⑥ 爬虫APP逆向实战教程包,含45项绝密技术详解
⑦ 超300本Python电子好书,从入门到高阶应有尽有
⑧ 华为出品独家Python漫画教程,手机也能学习
⑨ 历年互联网企业Python面试真题,复习时非常方便
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。