CKTIS / Python
bytearray()
Crée un tableau mutable d’octets.
Version : Python 3.14; repository image, production runtime not attested
Syntaxe
bytearray()
bytearray(source)
bytearray(string, encoding, errors="strict")Crée un tableau mutable d’octets.
Affecter 65 à la position zéro remplace le premier a par A, donc bytes(data) contient Abc. Avec l’entier 3 le constructeur crée trois octets nuls.
Affecter une valeur hors de 0–255 lève ValueError.
data = bytearray(b"abc")
alias = data
data[0] = 65
assert bytes(alias) == b"Abc" and bytearray(3).hex() == "000000"
Exemples
data = bytearray(b"abc")
data[0] = 65
print(bytes(data))
print(bytearray(3).hex())
Résultat
b'Abc' 000000
Affecter une valeur hors de 0–255 lève ValueError.