r/Python • • Jan 05 '18

Really??

Post image
2.0k Upvotes

317 comments sorted by

View all comments

Show parent comments

4

u/scootstah Jan 06 '18

Trying to read through a code flow in a sufficiently large code base with a dynamically typed language is a nightmare

Honestly, that just sounds like a badly designed application. I doubt adding strict type will make any difference to its legibility.

7

u/TravisJungroth Jan 06 '18
def func(obj):
    obj.method()

What does method() do? What other methods are available on obj? Without some sort of type enforcement, you don't have a clue without looking at the usage of func. This is fine when it's only used three times, but terrible when it's used a hundred times.

5

u/scootstah Jan 06 '18

Use good naming conventions and docblocks.

1

u/TravisJungroth Jan 06 '18

If I want to actually know the behavior of a method, I need to see the code. Even the most descriptive name represents a trivial amount of code. And doc strings don't help, cause those live with the code anyway.

You still have to look at the usage of func to find the code. To prevent that, your method names wouldn't need to be just good, but unique enough to define an interface for you. I just don't think the combination of method names alone is a good way to define an interface. It's too easy to conform to one by accident, and the method names have to get super long to present this.

It's better to define and enforce this interface through code (Abstract Base Classes, in Python's case). Then you can use type hinting to help as well. Try to find Alex Martelli's essay on "Goose Typing". It's in Fluent Python, but you might be able to find it otherwise.

3

u/scootstah Jan 06 '18

You don't know any more about a method based solely on its interface. You still need descriptive names.