What's the best way to minimize the need of run time checks?

Chris Angelico rosuav at gmail.com
Tue Aug 9 14:57:40 EDT 2016


On Wed, Aug 10, 2016 at 4:51 AM, Michael Selik <michael.selik at gmail.com> wrote:
> "File-like" is a good example. Rather than go through the frustration of a
> formal definition for what is file-like, stay productive and flexible with
> duck typing. Objects that don't have the appropriate methods or attributes
> will cause a TypeError or AttributeError. If you're passing in the wrong
> kind of object, you'll find out almost as soon as you write the code
> (because you test early and often), just as you would with a statically
> analyzed type system.

It's slightly delayed; with Python, you have to actually execute the
code path in question, whereas a static type system would detect it at
the compilation stage. So it may depend on the quality of your
testing. MyPy may be able to help there.

ChrisA



More information about the Python-list mailing list