夏夜寂寞属壁虎对《Fluent Python, 2nd Edition》的笔记(5)

Fluent Python, 2nd Edition
  • 书名: Fluent Python, 2nd Edition
  • 作者: Luciano Ramalho
  • 副标题: Clear, Concise, and Effective Programming
  • 页数: 850
  • 出版社: O'Reilly Media, Inc.
  • 出版年: 2021-1-1
  • 1. The Python Data Model

    In contrast, consider Go. Some objects in that language have features that are magic, in the sense that we cannot emulate them in our own user-defined types. For example, Go arrays, strings, and maps support the use brackets for item access, as in a[i]. But there’s no way to make the [] notation work with a new collection type that you define. Even worse, Go has no user-level concept of an iterable interface or an iterator object, therefore its for/range syntax is limited to supporting five “magic” built-in types, including arrays, strings, and maps. Maybe in the future, the designers of Go will enhance its metaobject protocol. But currently, it is much more limited than what we have in Python or Ruby.
    引自第1页

    Go 哈哈哈哈哈哈哈

    2022-06-13 18:30:53 回应
  • 9. Decorators and Closures

    装饰器模式的更多应用也可以参考陈皓的《Python修饰器的函数式编程》,评论也值得一看:https://coolshell.cn/articles/11265.html#%E7%BB%99%E5%87%BD%E6%95%B0%E8%B0%83%E7%94%A8%E5%81%9A%E7%BC%93%E5%AD%98

    更多 python decorator 代码示例:https://wiki.python.org/moin/PythonDecoratorLibrary

    2022-06-25 19:22:25 回应
  • 10. Design Patterns with First-Class Functions

    这章讲的两个重构场景都很精妙,活用了设计模式,这就是设计模式该用到的场合啊!(为什么要特地写读书笔记来提,就是因为我觉得这章的例子值得反复揣摩,即使不用 python 也很有参考价值)

    我的想法和 “‘Design Patterns’ Aren’t” 里面讲到的设计模式概念是一致的,设计模式不是固定的代码模板。

    2022-06-25 20:00:50 回应
  • 用解引用获取 collections objects 内容的一点小技巧

    书上似乎没讲解引用(*)可以用到的场合,我也不太清楚这些技巧放到哪一节比较合适,但还是做一点补充。

    ```

    # 不必要的做法

    target_list = [i for i in range(1, 10)]

    # 解引用获取元素列表

    target_list = [*range(1, 10)]

    # 调用 list() 获取元素列表

    target_list = list(range(1, 10))

    # 再对 range 用一次解引用,这种用法同样适用于其它一维 collections objects (具体用法有细微的区别)

    first, second, *remained = range(1, 10)

    >>> first

    1

    >>> second

    2

    >>> remained

    [3, 4, 5, 6, 7, 8, 9]

    >>> new_list = [*remained, second, first]

    [3, 4, 5, 6, 7, 8, 9, 2, 1]

    first, *middle, last = range(1, 10) # 只适用于 python3

    >>> first

    1

    >>> middle

    [2, 3, 4, 5, 6, 7, 8]

    >>> last

    9

    # 矩阵转置的一种写法

    a = [[1, 2, 3], [4, 5, 6]]

    list(list(x) for x in zip(*a))

    # 对 defaultdict 解引用,同样适用于其它二维 collections objects

    # 注意 new_dict 是 dict 类型,dict 和 collections.defaultdict 类型不一样

    # default_dict 没那么容易转换成 dict

    from collections import defaultdict

    x = defaultdict(lambda: '')

    x[1] = 'a'

    x[2] = 'b'

    >>> new_dict = { 3: 'c', 5: "abc", **x }

    {3: 'c', 5: 'abc', 1: 'a', 2: 'b'}

    ```

    解引用的基础用法可以参考 https://stackoverflow.com/questions/36901/what-does-double-star-asterisk-and-star-asterisk-do-for-parameters

    2022-06-26 23:05:27 回应
  • V. Metaprogramming

    这几章的内容和我想的不一样,然而又看了几篇讲元编程的博客发现是我没有准确把握元编程的概念。之前受到 C++ 模板元编程先入为主的影响,对元编程的理解偏向于“编译期产生代码”的细枝末节,现在看来偏离了“用元代码生成新代码”的重点。这么看来这章会提到装饰器其实很自然。

    元编程不是那么玄乎的东西,虽然无论哪个语言都会把相关内容放到高级特性里面,但是都离不开“用元代码产生新代码,从而尽量复用代码”这一点。Rust 派生宏是过程宏的一种,而过程宏也是元编程的一种具体实现方式,举个例子:

    ```

    /// 用派生宏简便地实现 Clone trait

    #[derive(Clone, Debug)]

    struct Student {

    name: String,

    classmates: Vec::<Student>,

    }

    impl Student {

    pub fn new(_name: String) -> Self {

    Student {

    name: _name,

    classmates: Vec::new(),

    }

    }

    pub fn add_single_classmate(&mut self, classmate: Student) {

    self.classmates.push(classmate);

    }

    }

    fn main() {

    let mut tom = Student::new("Tom".to_string());

    println!("{:?}", tom);

    let mary = Student::new("Mary".to_string());

    tom.add_single_classmate(mary); // 这里的实参应该是 mary.clone(),否则会报错

    println!("{:?}", mary); // borrow of moved value: 'mary'

    }

    ```

    Rust 不会给自定义的结构实现复制功能,也没法直接用 to_owned()、clone() 这些函数,所以需要手动实现 Clone 特性(其实是显式的深度拷贝)。Rust 提供了简单的封装,只要加上派生宏,rustc 编译器就会自动生成为 Student 实现 Clone 特性的代码。

    2022-07-08 10:19:28 回应

夏夜寂寞属壁虎的其他笔记  · · · · · ·  ( 全部179条 )

咖啡行者的全息烘焙法(第二版)
6
花衣魔笛手
3
我将宇宙随身携带
4
Programming Rust, 2nd Edition
2
程序员的职业素养
1
代码2.0
2
我与地坛
1
程序员修炼之道(第2版)
2
C++语言的设计和演化
3
古希腊思想通识课:希罗多德篇
1
R语言实战
1
权力意志与永恒轮回
2
代码大全(第2版)
2
TCP/IP网络编程
1
程序员的自我修养
5
历史学十二讲(增订本)
2
深入理解计算机系统(原书第3版)
1
The Complete Software Developer's Career Guide
1
哲学是做出来的
3
深度探索C++对象模型
2
重构(第2版)
3
七周七语言
1
蒙田随笔
3
你想活出怎样的人生
3
历史的巨镜
1
Unix/Linux编程实践教程
2
深入Linux内核架构
1
社会学之思(第3版)
1
C++ Primer
1
Operating Systems
1
深入理解计算机系统(原书第2版)
2
C专家编程
1
C Primer Plus
2
やがて君になる 佐伯沙弥香について(3)
1
Selected Poems of Dickinson
3
On Love and Loneliness
3
数学之美 (第二版)
1
中国近代思想史论
1
家庭、私有制和国家的起源
5
我的孤独是一座花园
1
鲁拜集
1
家庭、私有制和国家的起源
1
禅与摩托车维修艺术
4
Mother, Madonna, Whore
1
卡尔·波普尔
1
裴洞篇
2
劝学篇
4
计算机病毒防范艺术
1
30天自制操作系统
5
黑客
1
黑客与画家
2
复杂性思想导论
4
汇编语言(第3版)
1
The C Programming Language
4
阿拉伯─伊斯兰文化史(第一册)
3
The Communist Manifesto
3
论优美感和崇高感
1
How Behavior Spreads
2
同性恋研究
3
沙漏做招牌的疗养院
3
现代性与大屠杀
7
麦克白
1
道德形而上学的奠基
1
极权主义的起源
3
自由与多元论
5
社会运动、政治暴力和国家
6
こころ
4
Justice
1
Word Power Made Easy
1
JavaScript
8
Political Philosophy
8