Author Archives: admin

Grasping the nuclear fourth rail of Python syntax with both hands and holding on for dear life

In Python vs. Ruby: A Battle to The DeathGary Bernhardt wishes for Ruby-style blocks in Python.

The BDFL has already weighed in on anonymous blocks in Python:

If you want anonymous blocks, by all means use Ruby. Python has first-class callables,1 Ruby has anonymous blocks. Pick your favorite, but don’t whine that neither language has both. It ain’t gonna happen.

This seems to imply that first-class callables and anonymous blocks are mutually exclusive language features, but that’s wrong: JavaScript has the ability to pass callables around like anything else, and it has anonymous functions, which can be used just like Ruby’s anonymous blocks. Does that mean JavaScript is better than Python or Ruby? My feelings about Ruby are indifferent with a chance of rain, but I love Python, so I’ve got to ask: are you going to take this lying down, Python?

I’m not sure Python needs full-blown, Ruby-style anonymous blocks. But it might be good enough to be able to use function definitions as r-values, like JavaScript can. (If you’re not already wearing your tinfoil hat, now might be a good time to put it on.)

This would allow asynchronous code to be written in conceptual order (just like in JavaScript):

do_something_asynchronous(param1, param2, (def callback(result):
    do_something_with_result(result)
))

And it would allow encapsulation of lexical scope inside specific iterations of a loop to be used later when an asynchronous call returns (just like in JavaScript):

for item in results:
    (def single_iteration():
        do_something_asynchronous(param1, item, (def callback(result):
            do_something_with_result_and_item(item, result)
        ))
    )(item)

I’ve even had occasion to want that other Python namespace, class, to operate as an r-value:

class MyConverterClass(BaseConverterClass):
    my_converter_type = (class MyConverterType(float):
        def __str__(self): # a custom __str__ method
            return '%0.2f' % self
    )

In these examples I’ve wrapped their inline definitions in Python’s great syntactical equalizer, the parenthesis. It would be even nicer to be able to leave them off, but I’m sure that this syntax would run headlong into Python’s whitespace-based block definitions, and it would be even more of a train-wreck without parentheses.

I’ve also named the defs and classes. It would also be nice to be able to omit the function or class names if they were unneeded (just like in JavaScript). But anonymous functions, a.k.a. lambdas, are the electric third rail of Python syntax, so anonymous classes would be… I dunno, the nuclear fourth rail?

  1. Thanks to Steve for explaining that by “first-class callable,” GvR means functions that can be passed around and assigned to other variables without being executed. He also pointed out that the reason Ruby’s callables aren’t first-class is because optional parentheses in function calls leave no way to refer to a function without calling it, not because of the existence of anonymous blocks. []

Fuchsia, baige [sic], puke, butter yellow, pistachio…

In a fascinating bit of amateur lexical analysis, Stephen Von Worley created this color strata image, using data collected from XKCD‘s Color Survey:

Sadly, pistachio, a hue that’s notoriously difficult to pin down, is nowhere to be found. Spencer Finch will be disappointed.

If anyone’s interested in the actual linguistics behind color names, Berlin & Kay’s Basic Color Terms (Amazon link) is the seminal work, although I’m pretty sure they didn’t discover color names like baige [sic], puke, or butter yellow.

Just because it’s lost doesn’t mean it can’t be stolen

When normal people find something left behind, in a place of business (like a bar), that someone will likely want to have back (like an iPhone), they leave it with someone who works there (like the bartender). Then, the person who left it behind has a way to check if anyone has returned it. Taking it is stealing.

Gizmodo probably suspected that the prototype iPhone they bought was stolen. By now, they’ve made back the purchase price in page views, with a healthy return on top to help cover legal fees.

And if you think any of this will hurt the new iPhone’s sales, I have a bridg^H^H^H^H^Hphone to sell you.

Seen any good tables lately?

The periodic table fad has gone meta, not only with The Periodic Table Table at (Wake Forest University, in North Carolina):

But also with this periodic table of periodic tables1 by keaggy.com:

Before the Periodic Table of the Elements, elements were a chaotic collection of substances with seemingly random and unpredictable masses, melting and boiling points, electrical, chemical, and material properties, and so on. Not only did the periodic table organize the known elements in a way that explained these properties, but it correctly predicted the existence and the properties of undiscovered elements.

All these parodies of periodic tables2 are only funny because this tendency towards scientific organization has totally permeated popular culture. And that’s pretty encouraging.

  1. For those of you who hate your web browser and want it to crash, there’s also a version of the Periodic Table of Periodic Tables that uses an off-the-shelf Flash app and 217% of your CPU to make a zoomable interface that could be done in about ten lines of CSS & JavaScript, and 0% of your CPU. []
  2. My very own Periodic Table of the Europeans is at “Eu,” (number 63), smack dab in the middle of the “Awesomeoids” (the green row). Thanks, keaggy. []