Breakdown of approaches to PEP318

Sean Ross sross at connectmail.carleton.ca
Sun Mar 28 11:59:03 EST 2004


Here's a breakdown of most of the syntax discussed re: PEP318 using an
example from python-dev. There are probably several more (I've added [10]).
The example applies both function decoration and annotation.


# [1] .. [3], competing ways to say the same thing

# [1]
def foo(self) [attrs(spark_rule="DOC := HEAD BODY FOOT",
        url="/cgi-bin/directory/directory"),
        publish_to_web]:
        "doc string"
        body

# [2]
def foo[attrs(spark_rule="DOC := HEAD BODY FOOT",
        url="/cgi-bin/directory/directory"),
        publish_to_web](self):
        "doc string"
        body

# [3]
def [attrs(spark_rule="DOC := HEAD BODY FOOT",
        url="/cgi-bin/directory/directory"),
        publish_to_web] foo(self):
        "doc string"
        body


# [4] now once with 'as' - sub in your 'favourite': "using", "with", ...
def foo(self)  as attrs(spark_rule="DOC := HEAD BODY FOOT", \
        url="/cgi-bin/directory/directory"), \
        publish_to_web:
        "doc string"
        body


# [5] function attribute dictionary
def foo(self)[publish_to_web]:
        {spark_rule:"DOC:= HEAD BODY FOOT",
         url="/cgi-bin/directory/directory"}
        "doc string"
        body


# [6]  @function_attribute = ...
def foo(self)[publish_to_web]:
        @spark_rule="DOC:= HEAD BODY FOOT"
        @url="/cgi-bin/directory/directory"
        "doc string"
        body


# [7] :function_attribute:...
def foo(self)[publish_to_web]:
        :spark_rule:"DOC:= HEAD BODY FOOT"
        :url:"/cgi-bin/directory/directory"
        "doc string"
        body



# [8] special purpose blocks - several variations proposed
with this:
        spark_rule = "DOC:= HEAD BODY FOOT"
        url = "/cgi-bin/directory/directory"
using:
        publish_to_web
def foo(self):
        "doc string"
        body


# [9] some form of doc string abuse - one syntax option ...
def foo(self):
       """doc string
           @:
                    foo.spark_rule="DOC:=HEAD BODY FOOT",
                    foo.url="/cgi-bin/directory/directory")
                    foo = publish_to_web(foo)
       """
       body





# [10] metaclass approach - only works inside classes
decorate("foo", publish_to_web,
        spark_rule="DOC:=HEAD BODY FOOT",
        url="/cgi-bin/directory/directory")

def foo(self):
        "doc string"
        body



# [11] current method
def foo(self):
       "doc string"
       very
       long
       body
       ....
foo.spark_rule = "DOC:=HEAD BODY FOOT",
foo.url="/cgi-bin/directory/directory")
foo = publish_to_web(foo)





Personally, I find all of this very off-putting. I find none of these
proposals clean, clear, and/or readable, except perhaps [11] (which started
the whole mess in the first place). I tend to agree with the following,
somewhat out of context, remark:


"""
If only we could learn from others' mistakes:

Finally, in designing Self, we have learned one lesson by making mistakes:
examples can persuade the designer to include additional features which
later turn out to produce incomprehensible behavior in unforeseen
circumstances. This might be called the language designer's trap.
Minimalism, simplicity and consistency are better guides. They benefit every
programmer, not just the ones who need advanced features. We suspect that
many of today's object-oriented languages could profit by dropping features.

Programming as an Experience: The Inspiration for Self
"""
Isaac Gouy, http://lambda.weblogs.com/discuss/msgReader$11653





More information about the Python-list mailing list