from pymysql._compat import range_type, text_type, PY2 def _ensure_bytes(x, encoding=None): if isinstance(x, text_type): x = x.encode() #将str转化成byte elif isinstance(x, (tuple, list)): # x = (_ensure_bytes(v, encoding=encoding) for v in x) #不加type,在返回a列表的第一个元素1后就return,程序运行完成,打印的是个对象地址 x = type(x)(_ensure_bytes(v, encoding=encoding) for v in x) #加type,在返回a列表的所有元素后就return,程序运行完成,打印结果[1,‘2‘,9] # x = type(b)(_ensure_bytes(v, encoding=encoding) for v in x) #除了可以转换成列表还可以转换成字典,打印结果(1, b‘2‘, 9) return x a = [1,‘2‘,9] # b = () print(_ensure_bytes(a)) print(type(a))
原文:https://www.cnblogs.com/lelexiong/p/10103760.html