python关于type()的用法

如果按这种形式写  type(a)(b)
那此处的b是个可迭代对象,这个对象迭代完成后,再放到type里 
 
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))

猜你喜欢

转载自www.cnblogs.com/lelexiong/p/10103760.html
今日推荐