Jyotsna Masand
Jyotsna Masand

Reputation: 103

When I use sqlite3.connect('database_name.db'), how is Connection object made even when we do not explicitly specify the class name, Connection?

import sqlite3 conn=sqlite3.connect('jobs.db')

When the above lines are executed, we find that conn is an object of Connection class, which is present in sqlite3 python module.

The description of conn is as follows:

conn Connection (<class 'sqlite3.Connection'>) <sqlite3.Connection at 0x7fe12f6f3490>

Even though we did not explicitly specify in the code that conn is a Connection object, how does the interpreter determine that connect() is a function present in Connection class inside the module, and a Connection object must be prepared?

Upvotes: 0

Views: 592

Answers (1)

Harris Minhas
Harris Minhas

Reputation: 790

If we read the docs of sqlite3, we find that it mentions,

Opens a connection to the SQLite database file database. By default returns a Connection object, unless a custom factory is given.

Therefore, the developers have made this the default setting. You can however, provide your own custom factory.

Upvotes: 1

Related Questions