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
I've never used empty classes, but I think that might be the issue. I've only seen what you do on exceptions, and just for identifying them, not "use" them. Try something like this:
That way, you make sure that your object is constructed from the base class you want.
Edit: Fix the code I give you to your particular case, I'm writing from memory while I wait for a bus, so I might have missed something 🥲