[Python-ideas] Syntax for easy binding __name__, __module__, __qualname__ to arbitrary objects

Greg Ewing greg.ewing at canterbury.ac.nz
Tue May 14 02:03:13 CEST 2013


Martin Morrison wrote:
> Def is not a constructor. It is an assignment statement.

It's really some of both. It constructs a function
object, and binds it to a name. These two operations
are entwined, in a sense, because the name being
bound to is used as an argument in the construction.

We're after a generalisation of this entwined
construction-and-assignment. (Astruction? Consignment?)
I'm not entirely happy with the current proposal:

    def name = expr

because it doesn't fully entwine. The expr can be a
constructor, but doesn't have to be, and even when it
is, the construction occurs separately from the
assignment. Also, it looks like an ordinary assignment
with 'def' stuck in front, which, as Guido points
out, seems somewhat random.

I'd like to propose something a bit different:


    def name as expr(arg, ...)

which would expand to something like

    name = expr(arg, ..., __name__ = 'name', __module__ = 'module')

For example,

    def Animal as Enum('cat dog platypus')

This reads quite naturally: "define Animal as an
Enum with these arguments."

Another example based on my own use case:

    def width as overridable_property("The width of the widget.")

(Yes, this is yet another, different use of the word "as",
but I don't see anything wrong with that. Small words in
English often don't mean much on their own and derive most
of their meaning from their context.)

-- 
Greg


More information about the Python-ideas mailing list