and 与 or:交回的是操作数本身
视频简介
minutes or 30 为什么把 0 换成了 30?这支视频把对象画成贴纸:Python 先判断左边的真假,or 遇真就停,and 遇假就停,交回的就是那个原始对象,不一定是 True 或 False。被短路的右边根本不求值:函数里的 print 不会出现,records[0] 的 IndexError 也被挡住。最后把默认值改成 30 if minutes is None else minutes。
minutes or 30 为什么把 0 换成了 30?这支视频把对象画成贴纸:Python 先判断左边的真假,or 遇真就停,and 遇假就停,交回的就是那个原始对象,不一定是 True 或 False。被短路的右边根本不求值:函数里的 print 不会出现,records[0] 的 IndexError 也被挡住。最后把默认值改成 30 if minutes is None else minutes。