Python Glossary Русский

all Python 3codecsterm

codecs.IncrementalEncoder

incremental codecs

The base class of incremental encoders: encode(text, final=False) encodes a chunk while keeping state between calls; reset() starts the stream over.

Syntax

class MyEncoder(codecs.IncrementalEncoder):
    def encode(self, input, final=False): ...

Instances of the base class do nothing: encode raises NotImplementedError; a working encoder comes from getincrementalencoder or lookup(name).incrementalencoder. Its state is not a character tail but what the stream has already been told: the BOM is written, an ISO-2022 shift is active. So one encoder must not be shared by two files: the second one ends up without a BOM.

Examples

import codecs
enc = codecs.getincrementalencoder('utf-16-le')()
print(isinstance(enc, codecs.IncrementalEncoder))  # → True
print(enc.encode('ab'))  # → b'a\x00b\x00'
try:
    codecs.IncrementalEncoder().encode('x')
except NotImplementedError:
    print('base class')  # → base class

See also

Open in the full glossarydocs.python.orgReport an inaccuracy