bytearray
ha tutti i soliti metodi str
. Puoi farlo come un mutabile str
(byte in Python3)
Mentre array.array è orientato alla lettura e scrittura di file. 'B' è solo un caso speciale per array.array
Si può vedere c'è una bella differenza guardando il dir()
di ogni
>>> dir(bytearray)
['__add__', '__alloc__', '__class__', '__contains__', '__delattr__',
'__delitem__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__',
'__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__',
'__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__',
'__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append',
'capitalize', 'center', 'count', 'decode', 'endswith', 'expandtabs', 'extend',
'find', 'fromhex', 'index', 'insert', 'isalnum', 'isalpha', 'isdigit', 'islower',
'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans',
'partition', 'pop', 'remove', 'replace', 'reverse', 'rfind', 'rindex', 'rjust',
'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip',
'swapcase', 'title', 'translate', 'upper', 'zfill']
>>> dir(array)
['__add__', '__class__', '__contains__', '__copy__', '__deepcopy__',
'__delattr__', '__delitem__', '__doc__', '__eq__', '__format__', '__ge__',
'__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__',
'__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__',
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__',
'__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append',
'buffer_info', 'byteswap', 'count', 'extend', 'frombytes', 'fromfile',
'fromlist', 'fromstring', 'fromunicode', 'index', 'insert', 'itemsize', 'pop',
'remove', 'reverse', 'tobytes', 'tofile', 'tolist', 'tostring', 'tounicode',
'typecode']
avrei davvero dire 'bytes' è il successore di' str' di Python 2.x, non 'bytearray'. –
Non è nemmeno così preciso. Python 2.x's 'str' è come un mix di str e byte di Python 3.x. – Broseph
@Broseph: beh, 'bytes' * è * un successore di 2.x's' str' che prende di mira la "sequenza di byte indifferenziata" use case ... – SamB