[Python-ideas] Idea : for smarter assignment?

David Mertz mertz at gnosis.cx
Fri Jul 21 13:59:14 EDT 2017

On Fri, Jul 21, 2017 at 10:19 AM, Brett Cannon <brett at python.org> wrote:

> On Fri, 21 Jul 2017 at 10:08 Jason H <jhihn at gmx.com> wrote:
>> I experimented with Python in college and I've been for close to 20 years
>> now. (Coming and going as needed) I love the language. But there is one
>> annoyance that I continually run into.
>> There are basically two assignment operators, based on context, = and :
>> a = 1
>> { a: 1 }
The `=` isn't an assignment operator, it's a *binding*.  The name 'a' gets
bound to the integer object "1" in your example.  Don't confuse this with a
language like C where it really is an assignment.  If I later write:

    a = 2

I haven't changed the "cell" that contains the integer object, I've rebound
the NAME `a` to a different object.

But you've left out quite a few binding operations.  I might forget some,
but here are several:

    import a   # bind the name `a` to a module object

    with open(fname) as a: pass   # bind the name `a` to a file handle

    for a in [1]: pass   # bind the name `a` to each of the objects in an
    # ... In this case, the net result is identical to `a=1`

    def a(): pass    # bind the name `a` to a function object defined in
the body

    class a: pass   # bind the name `a` to a class object defined in the

With a bit of circuitous code, you *can* use a dictionary to bind a
variable too:

    >>> globals().update({'a':1})
    >>> a

Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170721/d54e4991/attachment.html>

More information about the Python-ideas mailing list