# encoding: utf-8
"""
@author: lanxiaofang
@software: PyCharm
@file: 11_0212_supp_knowledge.py
@time: 2020/2/12 0:25
"""

# 字典代替switch
#
# def isSunday():
# return 'Sunday'
# def isMonday():
# return 'Monday'
# def isTuesday():
# return 'Tuesday'
# def isWednesday():
# return 'Wednesday'
# def isThursday():
# return 'Thursday'
# def isFriday():
# return 'Friday'
# def isSaturday():
# return 'Saturday'
# def defaultday():
# return 'UnKnowDay, maybe is only for you'
#
# replaceswitch = {
# 0: isSunday,
# 1: isMonday,
# 2: isTuesday,
# 3: isWednesday,
# 4: isThursday,
# 5: isFriday,
# 6: isSaturday,
# }
#
# days = input("输入数字0-6")
# day_name = replaceswitch.get(int(days), defaultday)() # 第二个参数:当输入的内容不是字典中的key时所返回的内容
# print(day_name)


# 列表推导式 set dict tuple 都可以
# a = [1, 2, 4, 8, 16, 32, 64, 128]
# b = [i*2 for i in a]
# print(b)

# a = [1, 2, 4, 8, 16, 32, 64, 128]
# b1 = [i*2 for i in a if i > 10]
# b2 = {i*2 for i in a if i > 10}
# b3 = (i*2 for i in a if i > 10)
# print(b1)
# print
# # print(b3)
# # print(type(b3))
# print(tuple(b3))

# a = {
# '喜羊羊': 5,
# '美羊羊': 3,
# '沸羊羊': 6,
# '懒羊羊': 4,
# '暖羊羊': 4,
# '慢羊羊': 15,
# '红太狼': 7,
# '灰太狼': 8,
# '小灰灰': 1,
# }
# b1 = {key for key, value in a.items() if value >= 4}
# b2 = {value: key for key, value in a.items() if value >= 4}
# b3 = [key for key, value in a.items() if value >= 4]
# b4 = (key for key, value in a.items() if value >= 4)
# print(b1)
# print(b2)
# print(b3)
# print(tuple(b4))


# None 空 ≠ 空字符串 空列表 0 False
# n = None
# a = ''
# b = 0
# c = []
# print(type(a), a == n, a is n)
# print(type(b), b == n, b is n)
# print(type(c), c == n, c is n)
# print(type(n))

# # 建议用这个
# if not a:
# print("1True")
# else:
# print('1False')
# # 而不是这个
# if a is None:
# print("2True")
# else:
# print("2False")
# -----------------------------------------------
# if not n:
# print("3True")
# else:
# print("3False")
#
# if n is None:
# print("4True")
# else:
# print("4False")

# None,'',[],0 --对应--> False
# print(bool(None))
# print(bool([]))
# print(bool(''))
# print(bool(0))
# def test1():
# pass
#
# def test2():
# return 0
#
# print(bool(test1))
# print(bool(test2))

# __len__()与__bool__
# class test3():
# pass
#
# print(bool(test3()))

# class test3():
# def __bool__(self):
# print('bool called')
# return False # 返回的只能是bool类型
# def __len__(self):
# print('len called')
# return 1 # 返回的是长度,只能是正整数
# # return False # 长度是0
# # return True # 长度是1
#
# # 当test3()中两个内置函数都有的时候,用len()\bool()哪个就调用哪一个
# # 当test3()中只有__bool__函数的时候,不能调用len()
# # 当test3()中只有__len__函数的时候,len()和bool()都会调用,返回非0、True时bool()为True,反之为False
# print(len(test3()))
# print(bool(test3()))

# Python 3 中__nonzero__被__bool__替代了
# Python 2 中才有__nonzero__
class test3():
# def __nonzero__(self):
# pass
def __bool__(self):
print('bool called')
return False # 返回的只能是bool类型
def __len__(self):
print('len called')
return 1 # 返回的是长度,只能是正整数

到今天《Python入门与实战》学习总结 完结~