[Python-Dev] PEP 359: The "make" Statement
Ian Bicking
ianb at colorstudy.com
Thu Apr 13 23:12:28 CEST 2006
Steven Bethard wrote:
> On 4/13/06, "Martin v. Löwis" <martin at v.loewis.de> wrote:
>
>>Steven Bethard wrote:
>>
>>>I know 2.5's not out yet, but since I now have a PEP number, I'm going
>>>to go ahead and post this for discussion. Currently, the target
>>>version is Python 2.6. You can also see the PEP at:
>>> http://www.python.org/dev/peps/pep-0359/
>>>
>>>Thanks in advance for the feedback!
>>
> [snip]
>
>>Would it be possible/useful to have a pre-block hook to the callable,
>>which would provide the dictionary; this dictionary might not be
>>a proper dictionary (but only some mapping), or it might be pre-initialized.
>
>
> Yeah, something along these lines came up in dicussing using the make
> statement for XML generation. You might want to write something like:
>
> make Element html:
> make Element head:
> ...
> make Element body:
> ...
>
> however, this doesn't work with the current semantics because:
>
> (1) dict's are unordered
> (2) dict's can't have the same name (key) twice
Is the body of the make statement going to work like the body of a class
statement? I would assume so, in which case (2) would be a given. That
is, if you can do:
make Element html:
title_text = 'foo'
make Element title:
content = title_text
del title_text
Then you really can't have multiple keys with the same name unless you
give up the ability to refer in the body of the make statement to things
defined earlier in that same body. Unless items that were rebound were
hidden, but still somehow accessible to Element.
> and so you can only generate XML/HTML where the order of elements
> doesn't matter and you never have repeated elements. That's not
> really XML/HTML anymore.
>
> You could probably solve this if you could supply a different type of
> dict-like object for the block to be executed in. Then we'd have to
> have a translation from something like::
>
> make <callable> <name> <tuple> in <mapping>:
> <block>
>
> to something like::
>
> <name> = <callable>("<name>", <tuple>, <namespace>)
>
> where <namespace> is created by executing the statements of <block> in
> the <mapping> object. Skipping the syntax discussion for the moment,
> I guess I have two problems with this:
>
> (1) It complicates the statement semantics pretty substantially
> (2) It breaks the parallel with the class statement since you can't
> supply an alternate mapping type for class bodies to be executed in
> (3) It adds some degree of coupling between the mapping type and the
> callable. For the example above, I expect I'd have to do something
> like::
>
> make Element html in ElementDict():
> make Element head in ElementDict():
> ...
> make Element body in ElementDict():
> ...
Maybe Element.__make_dict__ could be ElementDict. This doesn't feel
that unclean if you are also using Element.__make__ instead of
Element.__call__; though there is a hidden cleverness factor (maybe in a
bad way).
--
Ian Bicking / ianb at colorstudy.com / http://blog.ianbicking.org
More information about the Python-Dev
mailing list