Python Glossary Русский

Python 3.12+inspectterm

inspect.BufferFlags

buffer protocol

The enum.IntFlag flags passed to __buffer__ when a buffer is requested (memoryview and the like). Added in 3.12 along with __buffer__ in Python.

Syntax

def __buffer__(self, flags): inspect.BufferFlags(flags)

They matter when your class exposes a buffer via __buffer__(self, flags): the flags tell you what the consumer asked for, such as write access (WRITABLE). The method receives a plain int; wrap it in BufferFlags(flags) to read names. It is an IntFlag, so print() shows the number rather than the name; use .name. memoryview requests FULL_RO.

Examples

import inspect
F = inspect.BufferFlags
print(F.FULL_RO, F.FULL_RO.name)  # → 284 FULL_RO
print(F.WRITABLE in F.FULL, F.WRITABLE in F.FULL_RO)  # → True False
import inspect
class Buf:
    def __init__(self):
        self.data = bytearray(b'abc')
    def __buffer__(self, flags):
        print(inspect.BufferFlags(flags).name)
        return memoryview(self.data)
print(bytes(memoryview(Buf())))  # → FULL_RO / b'abc'

See also

Open in the full glossarydocs.python.orgReport an inaccuracy