I'm a C++/C# developer and recently I've started to learn Python. I think I understand the concept of duck typing right now except for one thing.
When I'm coding in C++ or C# I sometimes create completely unrelated classes that have similar methods with similar signatures, for example: Create(), Open(), Close(), ProcessData(), etc. It's not a big deal in C++ or C# (unless you want to mess with C++ templates), but in languages with duck typing like Python this could cause a disaster. Consider the following example:
class DbTransaction: def rollback(self): pass class SourceControlChangeset: def rollback(self): pass def rollback_transaction(transaction): transaction.rollback() transaction = DbTransaction() changeset = SourceControlChangeset() rollback_transaction(transaction) rollback_transaction(changeset) # Oops!
Maybe be example is a bit artificial, but I think this could happen pretty easily in a big project with many classes.
What should I do in this situation? I think that type checking is not "pythonic" so my only idea is to change naming convention for class methods like this:
class DbTransaction: def rollback_transaction(self): pass class SourceControlChangeset: def rollback_changeset(self): pass