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

推荐订阅源

V
Visual Studio Blog
I
InfoQ
H
Help Net Security
GbyAI
GbyAI
博客园 - 叶小钗
Recent Announcements
Recent Announcements
Engineering at Meta
Engineering at Meta
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
爱范儿
爱范儿
Y
Y Combinator Blog
L
LangChain Blog
腾讯CDC
酷 壳 – CoolShell
酷 壳 – CoolShell
WordPress大学
WordPress大学
Stack Overflow Blog
Stack Overflow Blog
F
Fortinet All Blogs
G
Google Developers Blog
Apple Machine Learning Research
Apple Machine Learning Research
The GitHub Blog
The GitHub Blog
T
The Blog of Author Tim Ferriss
博客园 - Franky
D
Docker
Jina AI
Jina AI
罗磊的独立博客

会走路的三百块

会走路的三百块 会走路的三百块 其他 | 会走路的三百块 会走路的三百块 Unity-业务框架 | 会走路的三百块 其他 | 会走路的三百块 part 4 踩坑记录 | 会走路的三百块 Unity-进阶知识 | 会走路的三百块 Unity-天命6源码 | 会走路的三百块 CICD | 会走路的三百块 part 1 linux的历史节点 | 会走路的三百块 C的编译器 | 会走路的三百块 字符串常量存储 | 会走路的三百块 第三章 数据和C | 会走路的三百块 类型转换和缩减 | 会走路的三百块 零散知识点 | 会走路的三百块 DNS选择机制 | 会走路的三百块 网络八股Cache | 会走路的三百块 第三章-数据和C | 会走路的三百块 一些零散的知识点 | 会走路的三百块 自顶向下 | 会走路的三百块 linux简单速通 | 会走路的三百块 设计图的概述 | 会走路的三百块 AI和自动化暂存 | 会走路的三百块 第一章 初识C语言 | 会走路的三百块 Games101 | 会走路的三百块 Unity-Lua补充 | 会走路的三百块 Unity-个人Demo | 会走路的三百块 Unity-基础知识 | 会走路的三百块 Unity环境搭建 | 会走路的三百块
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'