r/PythonLearning • u/RomfordNavy • Sep 22 '26
Subclass mysql.connector
Any idea what I am doing wrong when trying to subclass mysql.connector like this:
import mysql.connector
class Database(mysql.connector.MySQLConnection):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def connect(self, *args, **kwargs):
super().connect(*args, **kwargs)
db = Database(host = '...', database = '...', user = '...', passwd = '...')
db_conn = db.connect(host = '...', database = '...', user = '...', passwd = '...')
print(f"{db_conn=}")db_conn always returns None.
3
Upvotes
View all comments
Show parent comment
2
u/Gold_Record_9157 Sep 22 '26
The second method, the overload of
connect, shouldn't be necessary, but if you do include it, make sure you return it's value:If you don't include the
returnin a function, Python addsreturn Noneso every assignment would getNoneas a result.Also, because of the way Python classes work, the first argument for a method called from
supershould beself, I think. I'm 95% sure, so I included it in my sampleEdit: syntax