PEP 359: The "make" Statement

Steven Bethard steven.bethard at gmail.com
Fri Apr 14 03:30:12 EDT 2006


Nicolas Fleury wrote:
> Steven Bethard wrote:
>> Ok, I finally have a PEP number.  Here's the most updated version of 
>> the "make" statement PEP.  I'll be posting it shortly to python-dev.
>>
>> Thanks again for the previous discussion and suggestions!
> 
> I find it very interesting.
> 
> My only complaint is that it is limited to things that can be described 
> as a namespace, where the order of declaration is lost.  This problem is 
> also true with metaclasses, but it is more acceptable since they are for 
> classes.

Yep, this seems to be the biggest complaint.  I'm waiting for write 
access to the repository, but here's a clip from the upcoming update:

Open Issues
===========

...

Should users of the make statement be able to determine in which dict
object the code is executed?  The make statement could look for a
``__make_dict__`` attribute and call it to allow things like::

     make Element html:
         make Element body:
             make Element h1:
                 '''First heading text'''
             make Element h1:
                 '''Second heading text'''

where a normal dict object would not suffice since order and repeated
names must be allowed.  Assuming that the ``__make_dict__`` attribute
was called to get the dict in which the block should be executed, the
following code should make the above make statements work::

     class Element(object):

         class __make_dict__(dict):
             def __init__(self, *args, **kwargs):
                 self._super = super(Element.__make_dict__, self)
                 self._super.__init__(*args, **kwargs)
                 self.values = []
             def __getitem__(self, name):
                 try:
                     return self._super.__getitem__(name)
                 except KeyError:
                     return globals()[name]
             def __setitem__(self, name, value):
                 self._super.__setitem__(name, value)
                 if not name.startswith('__'):
                     self.values.append(value)

         def __new__(cls, name, args, edict):
             result = etree.ElementTree.Element(name)
             result.text = edict.pop('__doc__', None)
             print edict.values
             for element in edict.values:
                 result.append(element)
             return result


STeVe



More information about the Python-list mailing list