Home > Uncategorized > More like “cluster-duck typing”

More like “cluster-duck typing”

This post about Python from Ted Dziuba about how it can make him uneasy has really hit home with me.

In my C/Python class, a lot of people moaned about why they had to learn Python (more vehemently than why they had to learn Differential Equations), and would bring up the flaws of Python. Out of this, I came to realize that Python’s desirable features are, in part, its syntactic sugar. Things like list comprehensions and the ease of using for loops, as well as the built-in data structures: lists, tuples, dictionaries, and sets.

But I find that what doesn’t particularly attract me, and actually makes me nervous as it does Ted, is Python’s “Duck Typing“:

In computer programming, duck typing is a style of dynamic typing in which an object’s current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class. The name of the concept refers to the duck test, attributed to James Whitcomb Riley, which may be phrased as follows:

If it walks like a duck and quacks like a duck, I would call it a duck.

So, basically, it doesn’t matter whether an object actually is a duck, you just treat it like a duck and hope that you’re right. This sounds pretty sketchy to me, and Ted points out the potential pitfalls in his post, but what really gets me is the preemptive defense of the technique in the summary paragraph of the Wikipedia article:

Users of statically typed languages new to dynamically typed languages may want to add such static, (before run-time), type checks which defeats duck typing, constraining the language’s dynamism.

“Constraining dynamism”? ‘Scuse me, but what the fuck is that supposed to mean? Of course, it seems to be a sort of high-minded way of calling the statically-typed language users uneducated rubes who don’t understand the “dynamism” and need to put down their strongly-typed stone knives and well-structured bearskins and step into the warm, enveloping folds of Duck Typing. Or, something.

But, whatever. So you treat your objects like what you want them to be, and then you just deal with it when the situation arises that you didn’t get what you expected. And how do you do that? That’s right, exceptions.

EAFP

Easier to ask for forgiveness than permission. This common Python coding style assumes the existence of valid keys or attributes and catches exceptions if the assumption proves false. This clean and fast style is characterized by the presence of many try and except statements.

That’s right: you put your code in a “try:” block, and then put “except:” blocks after it to catch any exceptions or errors that might arise during the try block. How it is “cleaner and faster” to test for error conditions after executing the code instead of testing whether your input will cause an error condition is not clear to me. But I digress.

This is all making kind of sense until right about here when it breaks down: how do you tell what exceptions or errors your code might throw? In Python, there is no enforced way of determining this.

If you’re lucky, the guy on the project before you documented all the possible errors and exceptions in a nice descriptive comment at the top of the function. If you’re unlucky, the fresh-out-of-school kid who thinks Python is a pretty rad take on Java (the language of Computer Science) whose code throws a ShitJustGotRealException because he wasn’t sure exactly what might be the cause. Shit can get super-real when you treat ints like tuples, bro.

This is where the “asking for forgiveness” breaks down: if you don’t know what you did wrong, how can you ask to be forgiven? Short answer, you can’t. You just have to apologize for everything and go do something else.

And the guys at Python seem to know this is a problem: they implemented a tiny slice of a solution in their recent major 3.0, backwards-compatibility-shattering release, but all that it essentially does is give more room for the coder to add comments. It doesn’t add any kind of explicit type-checking and it sure won’t correct you if you start trying to treat a long like a String. After all, you expected a long, right?

Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

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: