Minority Opinions

Not everyone can be mainstream, after all.

Vague Python Ideas

leave a comment »

I recently spent some time perusing the python-ideas mailing list, particularly around the development of tulip, and a few ideas struck my fancy.

def __main__():

I’ve mentioned before that the standard scriptish prefix bugs me, but more recently I’ve noticed myself moving all logic out of an if __name__ == "__main__" block into a separate function.  Why not make that official?  The transition could be handled in a few steps:

  1. After loading the __main__ module, check to see whether it has a callable __main__ attribute.  If so, and if it hasn’t yet been called, call it.
  2. Give modules their real __name__ where appropriate.
  3. Remove the “hasn’t yet been called” hack.

I’m fine with parts of this process taking until Python 4, though I wouldn’t be surprised if the multiprocessing developers pushed it forward.

name := expression

A second assignment operator would be useful for while and elif conditions, but it was the discussion of a __bind__ operation that made it seem useful in its own right.  Basically, the statement above would translate into (expression).__bind__("name", locals()), where object.__bind__(self, name, namespace) would be implemented as namespace[name] = self.  (The translation doesn’t really have to call locals() to get the namespace, though.)

In general, it’s just a more expensive assignment statement.  The real power comes in letting an object know its canonical name.  The canonical examples here are namedtuple and ORM fields, which are usually passed the same names as they collect on assignment.

The object of the expression also discovers when it’s bound, and to what, either or both of which might be useful.  Something might want to raise an exception if it’s bound into an incompatible environment, for example, but considering that removing the colon bypasses that restriction, it might not be worth the code.

Finally, it can bind a new object in its place; in an example from the thread, NumPy array manipulation could use that signal to evaluate a chain of lazy operations.  Even more odd would be using it to unroll looped test cases, but it’s not likely to work well for functions.

Whether that power is worth the implementation cost, I’ll leave up to others.  In particular, I could be persuaded to implement it myself, but not on my own dime.  It may also have to fight against Greg’s secret plan.

Invisible async operations

The tulip discussion originally interested me as a potential way to move forward with a hobby project I’ve been neglecting.  At first, I had my own event loop, with support for calling tasks on network availability, at scheduled times, or in separate threads.  Eventually, I replaced it with Twisted, mostly to make it easier to support multiple protocols on the same port.  Since then, I’ve debated switching to something more like co-routines.  That such an event loop might become standard makes me happy.

The tulip project itself, though, highlights a design decision that might become a sticking point: yield from gets proliferated every time something could block.  Worse, some of the functions designed for iteration look just like functions you might call directly.  That makes me think that perhaps gevent has the right idea with its monkey patching:  Calls to certain socket interfaces are invisibly replaced with something that uses a magically created event loop under the hood.

Seriously, when would a coroutine yield control?  Mostly on network I/O, sometimes when sleeping either for a given duration or until a given time, occasionally on other blocking system calls, rarely while waiting for another thread to give it a result, and seldom explicitly just to be nice during a long calculation.  All of that blocking could be handled at a low level, by packing up the current frame as a closure and continuing it later.  Does it need to know that it’s going to be suspended?

Perhaps I’ve just been contaminated by having learned a bit of the Go language, but it would be nice to have it in the language I already know and love.


Written by eswald

19 Feb 2013 at 7:19 pm

Posted in Python, Technology

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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s