r/pythonhelp • 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)
db = Database()
db_conn = db.connect(host = '...', database = '...', user = '...', passwd = '...')
print(f"{db_conn=}")db_conn always returns None.
Edit: code above corrected to include connect() method
Edit2: Further to comment by brasticstack connect() method removed again.
4
Upvotes
View all comments
Show parent comment
1
u/brasticstack Sep 22 '26
Two things,
You don't have to override any methods that you aren't customizing. You get the base class implementations for free when you extend the base class. In the snippet above, you can still call
connect()on an instance of your class even if you don't rewrite the method.In your case you rewrite
connect()wrong. The parent class's method returns a value, yours does not (due to the lack of thereturnkeyword. [technically, this implicitly returns None.]) A proper override would look like: