[Python-ideas] Augmented assignment syntax for objects.

Steven D'Aprano steve at pearwood.info
Sat Apr 29 08:34:39 EDT 2017


On Fri, Apr 28, 2017 at 11:04:00PM +0100, Erik wrote:

> Isn't binding an object to a namespace the same operation that 
> assignment performs?

Yes.

> So it's a type of assignment, and one that doesn't require the name to 
> be spelled twice in the current syntax (and that's partly why I took 
> offense at a suggestion - not by you - that I was picking "random or 
> arbitrary" keywords. I picked it for that specific reason).

That was me.

You took *offense*?

For somebody who made it clear that you didn't care about this "self 
import name" suggestion, you sure are touchy to have it criticised.

Assignment is the least interesting and important thing that the import 
statement does. Many keywords do an name binding operation, not just 
import, but also:

    for, with, except, def, class 

and most relevant to my comment:

    del 

is actually defined by the documentation as a name binding operation. 

https://docs.python.org/3/reference/executionmodel.html

I'm sorry that the joke was too subtle, I did put a wink after it, but 
you deleted it from your reply.

The point is, import's name binding is the least interesting and least 
important thing that it does. We could have defined import to be a 
function that returns a module object, or a tuple of objects, and left 
the binding up to the caller:

    # What If import were a function instead of a statement?
    math = import("math")
    sin, cos = import("math", "sin", "cos")

That's rather similar to what __import__ does, but of course import as a 
statement is a much better design. Nevertheless, we could take away the 
name-binding aspect of import, and it would still be recognisable as 
performing an import: it would search the module cache, search the path, 
load modules, compile the code, create module objects, and all the other 
things needed to import a module. The name binding (assignment) is just 
an convenience.

But take away the *import functionality*, and "import" is no longer an 
appropriate name. Why is it called import when it doesn't import 
anything? Because of the name binding? Well, these keywords all do name 
binding too, why can't we use them?

    self for attr
    self except attr
    self def attr
    self class attr

etc. I'm sorry that my posting style rubbed you the wrong way and made 
you defensive, by I stand by my comment: chosing import as the keyword 
to do something completely unrelated to importing is an arbitrary 
choice, and we might as well choose del (or def, or for) and save some 
typing.

If Python were the sort of language to use more symbols rather than 
names, like Perl, we could invent a bind to the left arrow operator:

    self <- a, b, c

Well, that wouldn't work, because of operator precedence. But you get 
the idea. Or we could bind to the right:

    a, b, c -> self


Make of them what you will.


-- 
Steve


More information about the Python-ideas mailing list