Python Glossary Русский

all Python 3sqlite3term

sqlite3.PrepareProtocol

adapters and converters

A marker class for the adaptation protocol: sqlite3 passes it to an object's __conform__ method to ask what it wants to become for SQLite.

Syntax

def __conform__(self, protocol): if protocol is sqlite3.PrepareProtocol: ...

It does nothing on its own — you compare against it with is inside __conform__: if protocol is sqlite3.PrepareProtocol. This lets your class store itself without a global register_adapter, but if an adapter is registered for the class anyway it wins and __conform__ is never called.

Examples

import sqlite3
class Point:
    def __init__(self, x, y):
        self.x, self.y = x, y
    def __conform__(self, protocol):
        if protocol is sqlite3.PrepareProtocol:
            return f'{self.x};{self.y}'
conn = sqlite3.connect(':memory:')
print(conn.execute('SELECT ?', (Point(1, 2),)).fetchone()[0])  # → 1;2
sqlite3.register_adapter(Point, lambda p: 'адаптер')
print(conn.execute('SELECT ?', (Point(1, 2),)).fetchone()[0])  # → адаптер — он важнее __conform__
conn.close()

See also

Open in the full glossarydocs.python.orgReport an inaccuracy