zoukankan      html  css  js  c++  java
  • 流畅的Python,Fluent Python,书中错误记录。

    书p129页,示例5-15

    def clip(text:str, max_len:'int' =8) -> str:
        '''
        :param text: 在max_len前面或后面的第一个空格处截断文本
        '''
        end = None
        if len(text) > max_len:
            space_before = text.rfind(' ', 0, max_len)  # 从右向左找,rfind,对应max_len前面的第一个空格
            if space_before >= 0:
                end = space_before
            else:
                space_after = text.find(' ', max_len) # 找max_len的后面了
                if space_after >= 0:
                    end = space_after
        if end is None:   # 没有找到,很聪明定义了一个None的开关
            end = len(text)
        return text[:end].rstrip()
    

     if space_after >= 0: end = space_after

    缩进错误。

    p566页,示例A.2

    import sys
    
    MAX_BITS = len(format(sys.maxsize, 'b'))  # 确定位数,我是64位
    print('%s-bit Python build' % (MAX_BITS + 1))
    
    
    def hash_diff(o1, o2):
        h1 = '{:0>{}b}'.format(hash(o1), MAX_BITS)  # 取出哈希值,用2进制格式化输出,右对齐,空位用0填充
        # print(h1)
        h2 = '{:>0{}b}'.format(hash(o2), MAX_BITS)
        # print(h2)
        diff = ''.join('|' if b1 != b2 else ' ' for b1, b2 in zip(h1, h2))  # 通过zip压缩循环取值,对比是否相等
        # print(diff)   相等留空,不想等划线
        count = '|={}'.format(diff.count('|'))  # 统计不想等的个数
        width = max(len(repr(o1)), len((repr(o2))), 8)  # 确定起头的宽度
        # print(width)
        sep = '-' * (width * 2 + MAX_BITS)  # 最后的过度线,
        # print(sep)
        return '{!r:{width}}=>{}
      {}{:{width}} {} 
    {!r:{width}}=>{}
    {}'.format(
            o1, h1, ' ' * (width), diff, count, o2, h2, sep, width=width)
        # 这个格式化最骚,首先标题订宽度用width,接着输入原始数字o1,=>输出哈希值,第二行输出|竖线,后面输出不同的数量
        # 第三排逻辑跟第一排一样,最后换行输出------线,这个太骚的格式化输出了
    
    
    if __name__ == '__main__':
        print(hash_diff(1,True))
        print(hash_diff(1, 1.01))
        print(hash_diff(1.0, 1))
        print(hash_diff('a', 'A'))
        print(hash_diff('a1', 'a2'))
        print(hash_diff('sidian', 'sidian'))
    

     return '{!r:{width}}=>{} {}{:{width}} {} {!r:{width}}=>{} {}'.format( o1, h1, ' ' * (width), diff, count, o2, h2, sep, width=width)

    return的格式化输出中多了一个{}

    书P196-197页,示例8-17,代码在shell运行不正确:

    import weakref                                                                           
    
    In [214]: a_set = {0, 1}                                                                           
    
    In [215]: wref = weakref.ref(a_set)                                                                
    
    In [216]: wref                                                                                     
    Out[216]: <weakref at 0x107c0a290; to 'set' at 0x107f4c410>
    
    In [217]: wref()                                                                                   
    Out[217]: {0, 1}
    
    In [218]: a_set = {2, 3, 4}                                                                        
    
    In [219]: wref()                                                                                   
    Out[219]: {0, 1}
    
    In [220]: wref() is None                                                                           
    Out[220]: False
    
    In [221]: wref() is None                                                                           
    Out[221]: False
    

     原因是执行了wref(),对象{0, 1}上面的变量标签迅速上涨,但在Py文件可以运行。

    为什么具体上涨,水平有限,无法解释。

    该链接有具体实例:https://www.cnblogs.com/sidianok/p/12094479.html

    p566页,示例A.3

    memtest.py中的代码:

    else:
        print('Usage: {} < vector - module - to - test>'.format('XXX.py'))
        sys.exit(1)
    

     书中format内容为空,报错。

  • 相关阅读:
    【leetcode】Binary Search Tree Iterator
    【leetcode】Palindrome Partitioning II
    【leetcode】Best Time to Buy and Sell Stock III
    【leetcode】Best Time to Buy and Sell Stock II
    【leetcode】Longest Consecutive Sequence
    【leetcode】Factorial Trailing Zeroes
    【leetcode】Simplify Path
    【leetcode】Generate Parentheses
    【leetcode】Combination Sum II
    【leetcode】Combination Sum
  • 原文地址:https://www.cnblogs.com/sidianok/p/12061114.html
Copyright © 2011-2022 走看看