[Python-ideas] Make-statement [Re: Different interface for namedtuple?]
Guido van Rossum
guido at python.org
Mon Mar 7 04:11:47 CET 2011
On Sat, Mar 5, 2011 at 10:27 PM, Carl M. Johnson
<cmjohnson.mailinglist at gmail.com> wrote:
> 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.
Hm... I find that example pretty clear and don't think it is in much
need of improvement. I also don't think there's anything wrong with
using class -- after all each call to GIF() returns a new object whose
attributes are defined by the definition. I'd assume that adding
methods to the class would just work and be utterly unsurprising to
anyone familiar with basic classes.
> 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
> or an anonymous block handler
TBH I find such abuse of 'with' much more troubling.
> 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.
This I agree with. I have a feeling that last time around it sunk
primarily because people were trying to pile too many different
semantics onto the same syntax -- hopefully the infatuation with Ruby
anonymous blocks has deflated somewhat by now.
--Guido van Rossum (python.org/~guido)
More information about the Python-ideas