3.10 ORM example The following code is simplified skeleton showing how data descriptors could be used to implement an object relational mapping. The essential idea is that the data is stored in an external database. The Python instances only hold keys to the database’s tables. Descriptors take care of lookups or updates: We can use the Field class to define models that describe the schema for each table in a database: To use the models, first connect to the database: An interactive session shows how data is retrieved from the database and how it can be updated: 12 classField : def __set_name__ (self, owner, name): self.fetch =f'SELECT { name } FROM { owner.table } WHERE { owner.key } =?;' self.store =f'UPDATE { owner.table } SET { name } =? WHERE { owner.key } =?;' def __get__ (self, obj, objtype = None ): return conn.execute( self.fetch, [obj.key]).fetchone()[ 0] def __set__ (self, obj, value): conn.execute( self.store, [value, obj .key]) conn.commit() classMovie : table='Movies ' # Table name key ='title' # Primary key director =Field() year =Field() def __init__ (self, key): self.key =key classSong : table='Music ' key ='title' artist =Field() year =Field() genre =Field() def __init__ (self, key): self.key =key >>> Movie('Star Wars').director 'George Lucas' >>> jaws =Movie('Jaws') >>> f'Released in { jaws.year } by { jaws.director } ' 'Released in 1975 by Steven Spielberg' >>> Movie('Star Wars').director ='J.J. Abrams ' >>> Movie('Star Wars').director 'J.J. Abrams' >>> import sqlite3 >>> conn =sqlite3 .connect( 'entertainment.db ') >>> Song('Country Roads').artist 'John Denver'