0

I'd like to enhance my model's implementation by adding some consistency checks (this value can't be negative, this one can't be None, etc.)

I wondered if it were « Pythonic » to add some « type checking »-ish checks as well. For instance, check that the object that I put in my Car is indeed a SteeringWheel.

I do want to perform some kind of type check in order to catch errors as soon as possible. I want to know if I added a proper SteeringWheel the instant I put it in the Car, not when the Car takes its first turn (and then debugging my way through the car wreck until I find the erroneous assignation).

I read that it would be more in the Python's duck type philosophy to check the added object's attributes or methods instead of its type. I understand this principle, but that would mean that the model must be aware of how the object will be used in order to check that the given object is correct, and that creates a wrong dependency between Model and Controller.

Moreover, what attributes should be tested? wheel.rotate() can apply to a SteeringWheel, but also to a Tire.

Another way would be to let the Model as it is and surround every use of a Model object by a try-catch, expecting a for an AttributeError or a TypeError, and that would be quite cumbersome.

Maybe I'm wrong to want this, and if anyone can convince me that I have the wrong idea, please do. But if you consider I'm right to want a model that raises an error when it becomes inconsistent, can you tell me a way to do that other than using isinstance() ?

Thank you.

Mankalas
  • 270
  • 3
  • 13

1 Answers1

0

You may want to use AbstractBaseClasses (https://docs.python.org/2/library/abc.html) here. The rationale in pep 3119 (http://legacy.python.org/dev/peps/pep-3119/#rationale) is exactly about your kind of problem (ensuring an objet supports a given protocol without ad-hoc inspection nor rigid typecheck).

bruno desthuilliers
  • 68,994
  • 6
  • 72
  • 93
  • As far as I understood this PEP, ABC just allows a `__instancecheck__` method to be implemented by the developer and called by `isinstance()`. Except maybe for tricky cases, I can't see how this is better than the built-in `isinstance()`. – Mankalas Oct 23 '14 at 13:21
  • @Mankalas: There's more to it. The point is that you can define an ABC (=> the protocol you expect) in one package, a compatible class in a second package and register this second class as a valid implementation of your ABC in a third package. It means that any existing compatible class can be used without having to hardcode dependencies / inheritence etc. FWIW, since Python let you rebind methods and even change an object's class at runtime, even "strict" typechecking wont protect you from runtime errors anyway, since an object could change it's class after passing your typecheck – bruno desthuilliers Oct 23 '14 at 14:15
  • Thank you for the explanation. I does seem to solve my problem AND comply to the duck typing philosophy. – Mankalas Oct 24 '14 at 10:31