bytearray([source [, encoding [, errors]]])

中文说明:

bytearray([source [, encoding [, errors]]])返回一个byte数组。Bytearray类型是一个可变的序列,并且序列中的元素的取值范围为 [0 ,255]。

参数source:

如果source为整数,则返回一个长度为source的初始化数组;

如果source为字符串,则按照指定的encoding将字符串转换为字节序列;

如果source为可迭代类型,则元素必须为[0 ,255]中的整数;

如果source为与buffer接口一致的对象,则此对象也可以被用于初始化bytearray.。

版本:在python2.6后新引入,在python3中同样可以使用

In[2]: a = bytearray(3)
In[3]: a
Out[3]: bytearray(b'\x00\x00\x00')
In[4]: a[0]
Out[4]: 0
In[5]: a[1]
Out[5]: 0
In[6]: a[2]
Out[6]: 0
In[7]: b = bytearray("abc")
Traceback (most recent call last):
File "/root/python_dev/.pyenv/versions/3.4.4/lib/python3.4/site-packages/IPython/core/interactiveshell.py", line 2885, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-7-e2c0db524cd1>", line 1, in <module>
b = bytearray("abc")
TypeError: string argument without an encoding
In[8]: b = bytearray(b"abc")
In[9]: b
Out[9]: bytearray(b'abc')
In[10]: b[0]
Out[10]: 97
In[11]: b[1]
Out[11]: 98
In[12]: b[2]
Out[12]: 99
In[13]: c = bytearray([1, 2, 3])
In[14]: c
Out[14]: bytearray(b'\x01\x02\x03')
In[15]: c[0]
Out[15]: 1
In[16]: c[1]
Out[16]: 2
In[17]: c[2]
Out[17]: 3

001_bytearray的更多相关文章

随机推荐

  1. java中的clone

    .clone 要实现cloneable接口: .深度clone和浅度clone .对象.clone() 1. Clone&Copy      假设现在有一个Employee对象,Employe ...

  2. Asp.Net IEnumerable,ICollection,IList,List区别

    做C#的同学们,都知道,一类只能有一个继承类,但可以实现多个接口.这句话就告诉我们:IEnumerable,ICollection,IList,List区别了 首先我看看 IEnumerable: / ...

  3. Android基础_3 Activity相对布局

    相对布局要比前面讲的线性布局和表格布局要灵活一些,所以平常用得也是比较多的.相对布局控件的位置是与其周围控件的位置相关的,从名字可以看出来,这些位置都是相对的,确定出了其中一个控件的位置就可以确定另一 ...

  4. codevs 1137 计算系数

    什么时候NOIP也要出二项式定理了? 二项式定理+逆元. #include<iostream> #include<cstdio> #include<cstring> ...

  5. sublime3 常用功能总结

    介绍几个常见的功能: l 自动完成:自动完成的快捷键是Tab和Enter,如果在html文件中,输入cl按下tab或Enter,即可自动补全为class=””:加上zencoding后,更是如虎添翼, ...

  6. 【Unity3D】枪战游戏—弹孔设置

    以子弹为原点,发射射线,如果射线检测到障碍物,则返回射线与障碍物的碰撞点 在该点处实例化出弹孔贴图 void Update () { transform.Translate (Vector3.forw ...

  7. 【转】cocos2d-x 3x Sprite3D

    Sprite3D Sprite3D works in many ways like a normal Sprite. Sprite3D is a three-dimensional model tha ...

  8. mysql大内存高性能优化方案

    mysql优化是一个相对来说比较重要的事情了,特别像对mysql读写比较多的网站就显得非常重要了,下面我们来介绍mysql大内存高性能优化方案 8G内存下MySQL的优化 按照下面的设置试试看:key ...

  9. C# chart绑定数据的方式整理

    C#chart 画图曲线的条数决定是你的数据源也就Series.Series是对象 你动态创建就可以了. 一.数组, List 等简单Collection类型的方式 Series s1= new Se ...

  10. PagerSlidingTabStrip 高亮选中标题

    1.选中标题后,高亮标题@Override public void onPageSelected(int position) { setSelectTextColor(position); if (d ...