Search code examples
pythonpython-3.xpandaspymysqlpandasql

Python confusing arguments


Sometimes pythons dynamic typing system is confusing to understand why some things work. For example:

  • Why does pandas.read_sql() accept and work as intended on a pymysql.Connection object as returned from pymysql.connect()? Note that nowhere in pandas documentation do they note that a pymysql.Connection object is accepted.

I'm looking for some clarification on the above question and some advice on how to generalise towards similar situations.


Solution

  • read_sql is documented as taking a Connectable object. Strictly speaking, that means it would be an instance of SqlAlchemy.engine.Connectable or a subclass thereof, but the only thing that is important is that it behave like such an instance. Roughly speaking, you can pass anything that's "like" a Connectable, in that, for example, tt provides a connect method that takes the same kinds of arguments as sqlalchemy.engine.Connectable.connect.

    In other words, it's not the exact type that matters, but the interface to the type you pass. Python refers to this as duck typing: if an object looks like a duck and acts like a duck, it can be used by something that requires a duck, even if it's not, actually, a duck.