What's the best way to minimize the need of run time checks?
MRAB
python at mrabarnett.plus.com
Tue Aug 9 15:22:20 EDT 2016
On 2016-08-09 19:57, Chris Angelico wrote:
> 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.
>
Static checking won't tell you if, say, you're adding when you should be
subtracting, so you'd need to execute the code path in question anyway
during testing!
More information about the Python-list
mailing list