[Python-ideas] Make-statement [Re: Different interface for namedtuple?]

Carl M. Johnson cmjohnson.mailinglist at gmail.com
Sun Mar 6 07:27:33 CET 2011


If you go back and re-read PEP 359, a lot of the motivating examples
-- creating namespaces, setting GUI properties, templating HTML -- are
still compelling. If you add in the examples of ORM tables and
interactive fiction processors, I think what unites all the examples
is that the make statement is a way of creating a DSL from within
Python. Can using a DSL within Python be pythonic ,though? That's a
difficult question to answer. But, on the other hand, just because
there's no clean, non-magical way of making a DSL within Python
doesn't mean people won't try and indeed, we can see the results of
their trying out there today.

For example, see Biwako <http://martyalchin.com/2011/jan/20/biwako/> a
recent project which abuses metaclasses to create a declarative syntax
for processing file formats. Here's an excerpt from that page:

> For example, here’s a very simple Biwako class that will can parse part of the
> GIF file format, allowing you to easily get to the width and height of any GIF image.
>
> from biwako import bin
>
> class GIF(bin.Structure, endianness=bin.LittleEndian, encoding='ascii'):
>    tag = bin.FixedString('GIF')
>    version = bin.String(size=3)
>    width = bin.Integer(size=2)
>    height = bin.Integer(size=2)
>
> Now you have a class that can accept any GIF image as a file (or any file-like
> object that’s readable) and parse it into the attributes shown on this class.
>
> >>> image = GIF(open('example.gif', 'rb'))
> >>> image.width, image.height
> (400, 300)

So basically, the author of this project is calling GIF a "class" but
it's not something that really operates the way a normal class does,
because it's subclassing the magical bin.Structure class and
inheriting its metaclass.

With a little searching, you can find similar examples of abuse that
are centered around the with statement rather than metaclasses. People
have made the with statement into an XML generator
<http://langexplr.blogspot.com/2009/02/writing-xml-with-ironpython-xmlwriter.html>
or an anonymous block handler
<http://code.google.com/p/ouspg/wiki/AnonymousBlocksInPython>.

Seeing examples like these make me think a re-examination of PEP 359
would be a good idea. Of course, I do think it needs a little more
work (in particular, I think the make statement should have an
equivalent of a __prepare__ method and should receive the BLOCK as a
callback instead of automatically executing it), but the core idea is
worth taking another look at.

-- Carl



More information about the Python-ideas mailing list