python关于type()的用法

Posted 乐乐熊小妹

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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))

 

以上是关于python关于type()的用法的主要内容,如果未能解决你的问题,请参考以下文章

关于Python的super用法研究

关于Xpath的用法问题?

python调试:pdb基本用法(转)

c_cpp 加载源图像固定用法(代码片段,不全)

python 中的type

Python 关于supper 的 用法和原理(挖坑)