=Start=
缘由:
整理总结一下最近在编码过程中遇到的一些Python知识点,方便以后快速参考、学习。
正文:
参考解答:
1、Python中如何获取调用者的函数名
#!/usr/bin/env python # coding=utf-8 import time import sys # 建议使用下面这个函数,一方面速度快,一方面只需要引入sys模块即可 def whoami(): return sys._getframe(1).f_code.co_name def foo(): print(whoami()) def caller(): print("begin caller()") print(whoami()) # caller print("end caller()") def caller2(): foo() #foo print(whoami()) #caller2 def main(): caller() foo() #foo caller2() if __name__ == '__main__': time_start = time.time() try: main() except KeyboardInterrupt: print('Killed by user') sys.exit(0) print("Spend {0} seconds.\n".format(time.time() - time_start))
对应的测试代码:
$ python -m timeit -s 'import inspect, sys' 'inspect.stack()[0][0].f_code.co_name' 1000 loops, best of 3: 583 usec per loop $ python -m timeit -s 'import inspect, sys' 'inspect.stack()[0][3]' 1000 loops, best of 3: 583 usec per loop $ python -m timeit -s 'import inspect, sys' 'inspect.currentframe().f_code.co_name' 10000000 loops, best of 3: 0.159 usec per loop $ python -m timeit -s 'import inspect, sys' 'sys._getframe().f_code.co_name' 10000000 loops, best of 3: 0.156 usec per loop
2、Python中如何进行dict合并
- Python 3.5及以上版本
z = {**x, **y}
- Python 2或Python 3.4及以下版本
def merge_two_dicts(x, y): z = x.copy() # start with x's keys and values z.update(y) # modifies z with y's keys and values & returns None return z z = merge_two_dicts(x, y)
参考链接:
- https://stackoverflow.com/questions/5067604/determine-function-name-from-within-that-function-without-using-traceback
- https://stackoverflow.com/questions/33162319/how-can-get-current-function-name-inside-that-function-in-python
- https://stackoverflow.com/questions/38987/how-to-merge-two-dictionaries-in-a-single-expression
- https://segmentfault.com/a/1190000010567015
=END=
《 “Python的一些小知识点_13” 》 有 3 条评论
你应该使用Python3里的这些新特性
http://www.bugcode.cn/2019/05/20/python3_new_func/
`
f-strings (3.6+)
Pathlib (3.4+)
Type hinting (3.5+)
Enumerations (3.4+)
Built-in LRU cache (3.2+)
Extended iterable unpacking (3.0+)
Data classes (3.7+)
Implicit namespace packages (3.3+)
`
Python3的这些新特性很方便
http://www.bugcode.cn/2018/01/31/move_python2/
https://github.com/arogozhnikov/python3_with_pleasure
Python 3中的字符串格式化
Python 3’s f-Strings: An Improved String Formatting Syntax (Guide)
https://realpython.com/python-f-strings/
How To Use String Formatters in Python 3
https://www.digitalocean.com/community/tutorials/how-to-use-string-formatters-in-python-3
`
# 直接使用 {} 作为占位符
print(“Sammy has {} balloons.”.format(5))
# 使用一个字符串作为模板
open_string = “Sammy loves {}.”
print(open_string.format(“open source”))
# 多个「默认」占位符
new_string = “Sammy loves {} {}.” #2 {} placeholders
print(new_string.format(“open-source”, “software”)) #Pass 2 strings into method, separated by a comma
# 使用「显示指定顺序」的占位符
print(“Sammy the {1} has a pet {0}!”.format(“shark”, “pilot fish”))
# 使用「命名」占位符
print(“Sammy the {0} {1} a {pr}.”.format(“shark”, “made”, pr = “pull request”))
# 指定类型
print(“Sammy ate {0:.3f} percent of a pizza!”.format(75.765367)) # 75.765
print(“Sammy ate {0:.1f} percent of a pizza!”.format(75.765367)) # 75.8
# 变量填充
print(“Sammy has {0:4} red {1:16}!”.format(5, “balloons”))
print(“Sammy has {0:<4} red {1:^16}!".format(5, "balloons"))
print("{:*^20s}".format("Sammy"))
`
Python format 格式化函数
https://www.runoob.com/python/att-string-format.html
30秒内便能学会的30个超实用Python代码片段
https://mp.weixin.qq.com/s/aVK_vGKt0DISRnfkCHbiMA
`
1. 唯一性
2. 变位词(相同字母异序词)
3. 内存
4. 字节大小
5. 打印N次字符串
6. 首字母大写
7. 列表细分
8. 压缩
9. 计数
10. 链式比较
11. 逗号分隔
12. 元音计数
13. 首字母小写
14. 展开列表
15. 寻找差异
16. 输出差异
17. 链式函数调用
18. 重复值存在与否
19. 合并字库
20. 将两个列表转换为字库
21. 列举
22. 时间成本
23. Try else语句
24. 出现频率最高的元素
25. 回文(正反读有一样的字符串)
26. 不用if-else语句的计算器
27. 随机排序
28. 展开列表
29. 交换变量
30. 获取丢失部分的默认值
`
https://github.com/30-seconds/30_seconds_of_knowledge