1. 程式人生 > >python關於type()的用法

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))