[Python-Dev] Defining properties - a use case for class decorators?

Phillip J. Eby pje at telecommunity.com
Tue Oct 18 17:31:32 CEST 2005


At 11:59 PM 10/18/2005 +1000, Nick Coghlan wrote:
>An idea that was kicked around on c.l.p a long while back was "statement 
>local
>variables", where you could define some extra names just for a single simple
>statement:
>
>    x = property(get, set, delete, doc) given:
>        doc = "Property x (must be less than 5)"
>        def get(self):
>            try:
>                return self._x
>            except AttributeError:
>                self._x = 0
>                return 0
>...
>
>As I recall, the idea died due to problems with figuring out how to allow the
>simple statement to both see the names from the nested block and modify the
>surrounding namespace, but prevent the names from the nested block from
>affecting the surrounding namespace after the statement was completed.

Haskell's "where" statement does this, but the block *doesn't* modify the 
surrounding namespace; it's strictly local.  With those semantics, the 
Python translation of the above could just be something like:

         def _tmp():
            doc = "blah"
            def get(self):
                # etc.
            # ...
            return property(get,set,delete,doc)

         x = _tmp()

Which works great except for the part that co_lnotab won't let you identify 
that "return" line as being the original expression line, due to the 
monotonically-increasing bit.  ;)

Note that a "where" or "given" statement like this could make it a little 
easier to drop lambda.



More information about the Python-Dev mailing list