h1

IDuck

13 May, 2010

How do you build a large project in Python?

Here is what is bothering me: Programming is not just telling a computer what to do. Programming is also a conversation between developers. This conversation is happening through the structure of the code, the names of the variables, the design patterns if both developers also speak pattern. Developer 1 implements something. Developer 2 takes in Developer 1’s ideas, maybe argues back, makes changes and tries to keep it clear for Developer 3. Developers 1, 2 and 3 might be team members. Or they might be the same person at six month intervals.

In a statically typed language (say C# or Java) I can use interfaces as part of the conversation. If my method requires a passed-in object to implement a particular interface, that’s me explaining to you (or to myself six months later) how this works and making sure you don’t miss anything.

Python doesn’t have interfaces; you don’t need them when you have duck typing. This bothers me. If I want to express the idea that ‘the object passed in here must be able to do A, B and C’, I can write comments to that effect, but I can’t enforce it*. If, six months later, Future Me passes in an object that can do A and B, all will be well and good until one day something unexpected happens, C is called and the thing falls over.

Sure, back in the statically typed language, the next developer might sigh in frustration** at yet another ‘method not implemented’ compiler error, stub C, and we’re no better off. But I’ve done all I can, through the code***, to let them know that method C is required.

On a large Python project, how do you handle this? Must you trust all the subsequent developers to have the experience and rigour to find out about method C? Does duck typing only work if everyone is 100% awesome all the time?

This is a genuine question, not a dig at Python. I’m curious as to how this is handled.


* presumably, I could write code to check that methods A, B and C exist on the object, but that does not give me a warm fuzzy feeling.
** that’s ok as long as they don’t draw stickman-death pictures of me. Motto: ‘write code such that nobody draws stickman-death pictures of me on their whiteboard’. This is hard.
*** which, let’s be honest here, is the only reliable design document 🙂

Advertisements

One comment

  1. Bump!

    I overheard two PL geeks, with a total of about six compilers/interpreters to their credit, discussing this last week. They agreed that static typing is a pain to implement – but worth it, because the resulting code runs much faster.

    Which leaves the original question unanswered. Thoughts?



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: