惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园 - 三生石上(FineUI控件)
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
B
Blog
D
DataBreaches.Net
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
V
Vulnerabilities – Threatpost
Jina AI
Jina AI
T
Threat Research - Cisco Blogs
The Hacker News
The Hacker News
Latest news
Latest news
博客园_首页
T
Tenable Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
酷 壳 – CoolShell
酷 壳 – CoolShell
Apple Machine Learning Research
Apple Machine Learning Research
Spread Privacy
Spread Privacy
Martin Fowler
Martin Fowler
Y
Y Combinator Blog
P
Privacy & Cybersecurity Law Blog
C
Cisco Blogs
I
InfoQ
The Cloudflare Blog
J
Java Code Geeks
C
Cybersecurity and Infrastructure Security Agency CISA
量子位
P
Proofpoint News Feed
C
Cyber Attacks, Cyber Crime and Cyber Security
Last Week in AI
Last Week in AI
T
Tailwind CSS Blog
AWS News Blog
AWS News Blog
Stack Overflow Blog
Stack Overflow Blog
Hugging Face - Blog
Hugging Face - Blog
The Register - Security
The Register - Security
M
MIT News - Artificial intelligence
G
Google Developers Blog
Simon Willison's Weblog
Simon Willison's Weblog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
T
The Exploit Database - CXSecurity.com
A
Arctic Wolf
D
Darknet – Hacking Tools, Hacker News & Cyber Security
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
Visual Studio Blog
Project Zero
Project Zero
P
Privacy International News Feed
Engineering at Meta
Engineering at Meta
G
GRAHAM CLULEY
博客园 - Franky
C
CERT Recently Published Vulnerability Notes

会走路的三百块

其他 | 会走路的三百块 part 4 踩坑记录 | 会走路的三百块 Unity-进阶知识 | 会走路的三百块 Unity-天命6源码 | 会走路的三百块 CICD | 会走路的三百块 part 1 linux的历史节点 | 会走路的三百块 C的编译器 | 会走路的三百块 字符串常量存储 | 会走路的三百块 第三章 数据和C | 会走路的三百块 类型转换和缩减 | 会走路的三百块 零散知识点 | 会走路的三百块 DNS选择机制 | 会走路的三百块 网络八股Cache | 会走路的三百块 第三章-数据和C | 会走路的三百块 一些零散的知识点 | 会走路的三百块 自顶向下 | 会走路的三百块 linux简单速通 | 会走路的三百块 设计图的概述 | 会走路的三百块 AI和自动化暂存 | 会走路的三百块 第一章 初识C语言 | 会走路的三百块 Games101 | 会走路的三百块 Unity-Lua补充 | 会走路的三百块 Unity-个人Demo | 会走路的三百块 Unity-基础知识 | 会走路的三百块 Unity环境搭建 | 会走路的三百块 Unity-经典MMO | 会走路的三百块 Fastapi-Pydantic | 会走路的三百块 Fastapi-Sqlalchemy | 会走路的三百块 Scrapy-Redis分布式爬虫 | 会走路的三百块 Scrapy-Web集成 | 会走路的三百块 Scrapy爬虫 | 会走路的三百块 Twitter爬虫 | 会走路的三百块 Uinotes爬虫 | 会走路的三百块 Python库 | 会走路的三百块 Python快速复习 | 会走路的三百块 Python语言进阶 | 会走路的三百块 Py自动化办公 | 会走路的三百块 Py踩坑记录 | 会走路的三百块 深入Python | 会走路的三百块 基本前端常识 | 会走路的三百块 文档分发与产品安全 | 会走路的三百块 跨平台应用脚手架 | 会走路的三百块 后端开发常见层式结构 | 会走路的三百块 mongodb | 会走路的三百块 PG-ep1 | 会走路的三百块 分库分表 | 会走路的三百块 docker常用知识 | 会走路的三百块 K3S | 会走路的三百块 后端常见安全措施 | 会走路的三百块 part 2 常用命令 | 会走路的三百块 part 3 shell脚本 | 会走路的三百块 服务器装机 | 会走路的三百块 测试与性能指标评估 | 会走路的三百块 缓存系统设计 | 会走路的三百块 Caddy | 会走路的三百块 Nginx-EP1 | 会走路的三百块 Nginx-EP2 | 会走路的三百块 EP1-设计模式 | 会走路的三百块 EP2-创建型模式 | 会走路的三百块 EP3-结构型模式 | 会走路的三百块 EP4-行为模式 | 会走路的三百块 EP5-其他 | 会走路的三百块 软件工程 | 会走路的三百块 日志模块设计 | 会走路的三百块 订单和支付模块 | 会走路的三百块 minio对象存储 | 会走路的三百块 Rustfs对象存储 | 会走路的三百块 一次性搞懂 | 会走路的三百块 基本硬件知识 | 会走路的三百块 开源协议 | 会走路的三百块 彩蛋收集 | 会走路的三百块 版本控制 | 会走路的三百块 项目管理 | 会走路的三百块 leetcode-EP1 | 会走路的三百块 第二章 C语言概述 | 会走路的三百块 第零章 计算机概论 | 会走路的三百块 爬虫基础 | 会走路的三百块 Sqlite | 会走路的三百块 README | 会走路的三百块 异步任务模块 | 会走路的三百块 用户和验权 | 会走路的三百块 通用数据库执行器 | 会走路的三百块 clickhouse | 会走路的三百块 会走路的三百块 Obsidian博客搭建 | 会走路的三百块 关于 | 会走路的三百块
PythonCheatSheet | 会走路的三百块
2026-05-25 · via 会走路的三百块

创建于 2022年5月25日 17:42:46 · 约732字

[ 📝Note ]

积累一些技巧,比如读写超大文件这种小技巧

🕊python 进阶技能_哔哩哔哩_bilibili
Python 进阶视频_哔哩哔哩_bilibili

零散的记忆缓存 ​

好习惯 ​

  • 禁止使用"a"+b+"c"使用"a{b}c"
  • 不要except:空Error
  • 检查参数是否为不可变
  • 使用推导式注意可读性
  • 替换原则,子类代替父类
  • 不要用═ ═检查布尔值,用is,用if检查bool直接if x
  • 使用for i,v in enumerate(list)代替for i in range(len(list))获取索引+元素
    • 甚至可以zipfor i,(v1,v2) in enumerate(zip(list1,list2))
  • 约定优于配置
  • tuple解包x,y=atuple
  • 测试运行时间使用time.perf_counter时钟时间代替time.time更加精确
  • 试运行使用log模块而不是打印
  • 多进程不要总是shell=True,这会让进程在shell里运行(他是这么说的,但是我也不知道是啥问题)
  • 不要import *会命名空间混乱
  • python虽然是解释型语言但也不是不编译,他只是编译到字节码阶段
  • 遵守PEP8
  • 看到奇怪语法,想想是不是python2的遗留代码
  • 查看文件(夹)是否存在,使用os.path或pathlib里的函数,这样不用打开文件性能高
  • 不要在循环过程中修改可变类型etc数组
  • 两个字符串中间没东西会自动合并
  • 给俩变量一块赋值,发现不对劲检查括号
  • 使用tuple函数声明传入可迭代类型会挨个构建

命名 ​

驼峰法使用大小写来命名变量的方法,分为大驼峰和小驼峰
特点 大驼蜂: UserName

  • 第一个字母大写,后边也大写
    • 类名
    • 函数名
    • 属性名
    • 命名空间

小驼蜂userName

  • 第一个字母小写,后边大写
    • 变量

循环记得打括号

Pythonic ​

#coding=utf-8#-*- coding: utf-8 -*-

一直搞不清楚这俩头什么区别。直觉上一样。今天特意查了一下,大致明白了:
对于python解释器来说,这两种写法一样。但是对于编辑器来讲,可能会出现识别问题。

参考PEP-0236 Defining the Encoding

Click to see more

Python will default to ASCII as standard encoding if no other encoding hints are given.

To define a source code encoding, a magic comment must
be placed into the source files either as first or second
line in the file, such as:

      # coding=<encoding name>

or (using formats recognized by popular editors)

      #!/usr/bin/python
      # -*- coding: <encoding name> -*-

or

      #!/usr/bin/python
      # vim: set fileencoding=<encoding name> :

More precisely, the first or second line must match the regular
expression "coding[:=]\s*([-\w.]+)". The first group of this
expression is then interpreted as encoding name. If the encoding
is unknown to Python, an error is raised during compilation. There
must not be any Python statement on the line that contains the
encoding declaration.

To aid with platforms such as Windows, which add Unicode BOM marks
to the beginning of Unicode files, the UTF-8 signature
'\xef\xbb\xbf' will be interpreted as 'utf-8' encoding as well
(even if no magic encoding comment is given).

If a source file uses both the UTF-8 BOM mark signature and a
magic encoding comment, the only allowed encoding for the comment
is 'utf-8'.  Any other encoding will cause an error.

链式调用 ​

类中的一些函数没有返回值的时候,可以返回self使用方法链,如果是不修改自身实例,返回一个新的,可以使用type创建新实例使用方法链

链式调用

python

class Player:  
    def __init__(self,name,x,y):  
        self.name=name  
        self.x=x  
        self.y=y  
    def zero(self):  
        self.x=0  
        self.y=0  
        return self # 返回自身,使用链式调用  
    def moveX(self,offset):  
        self.x+=offset  
        return self # 返回自身,使用链式调用  
        # return type(self)(self.name,self.x+offset,self.x) # 不改变自身返回新对象的链式调用  
    def moveY(self,offset):  
        self.y+=offset  
        return self # 返回自身,使用链式调用  
    def __str__(self):  
        return "%s:(%s,%s)"%(self.name,self.x,self.y)  
  
p=Player("player1",5,5)  
(  
   p.zero()  # 也可以用\
    .moveX(3)  
    .moveY(1)  
)  
# 等价于 p.zero().moveX(3).moveY(1)
print(p)

运行结果

时间日期格式化 ​

python

date_obj = datetime.strptime(datetime_str,'%Y-%m-%dT%H:%M:%S.%fZ')  # 尝试将字符串解析为ISO日期
result_str = date_obj.strftime('%Y-%m-%d')  # 将日期转换为所需的格式'%y-%m-%d'