Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #93108

Could you explain this rebinding (or some other action) on "nums = nums"?

X-Received by 10.50.142.8 with SMTP id rs8mr457106igb.13.1435189930751; Wed, 24 Jun 2015 16:52:10 -0700 (PDT)
X-Received by 10.50.142.39 with SMTP id rt7mr4096igb.17.1435189930737; Wed, 24 Jun 2015 16:52:10 -0700 (PDT)
Path csiph.com!optima2.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!j8no905532igd.0!news-out.google.com!kd3ni16533igb.0!nntp.google.com!h15no8175612igd.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups comp.lang.python
Date Wed, 24 Jun 2015 16:52:10 -0700 (PDT)
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=76.27.220.131; posting-account=SZ_svQkAAACWRFG2bDA-zgq8ILyl4-vo
NNTP-Posting-Host 76.27.220.131
User-Agent G2/1.0
MIME-Version 1.0
Message-ID <94c2e42e-1e5f-40cf-9259-26035e277bf3@googlegroups.com> (permalink)
Subject Could you explain this rebinding (or some other action) on "nums = nums"?
From fl <rxjwg98@gmail.com>
Injection-Date Wed, 24 Jun 2015 23:52:10 +0000
Content-Type text/plain; charset=ISO-8859-1
Xref csiph.com comp.lang.python:93108

Show key headers only | View raw


Hi,

I read a blog written by Ned and find it is very interesting, but I am still
unclear it in some parts. In the following example, I am almost lost at the
last line: 

nums = num


Could anyone explain it in a more detail to me?

Thanks,





.......................
The reason is that list implements __iadd__ like this (except in C, not Python):

class List:
    def __iadd__(self, other):
        self.extend(other)
        return self
When you execute "nums += more", you're getting the same effect as:

nums = nums.__iadd__(more)
which, because of the implementation of __iadd__, acts like this:

nums.extend(more)
nums = nums
So there is a rebinding operation here, but first, there's a mutating operation, and the rebinding operation is a no-op.

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Could you explain this rebinding (or some other action) on "nums = nums"? fl <rxjwg98@gmail.com> - 2015-06-24 16:52 -0700
  Re: Could you explain this rebinding (or some other action) on "nums = nums"? Chris Angelico <rosuav@gmail.com> - 2015-06-25 10:16 +1000

csiph-web