Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #105856
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Terry Reedy <tjreedy@udel.edu> |
| Newsgroups | comp.lang.python |
| Subject | Re: Why lambda in loop requires default? |
| Date | Sun, 27 Mar 2016 12:12:39 -0400 |
| Lines | 45 |
| Message-ID | <mailman.89.1459095177.28225.python-list@python.org> (permalink) |
| References | <56F73B60.9020603@gmail.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=utf-8; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Trace | news.uni-berlin.de mgFFebFiP1SywNCo0CABYAO3etUkO3IVRhukNRl51uwg== |
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'essentially': 0.04; 'inspired': 0.05; 'puts': 0.07; 'derived': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'ruby,': 0.09; 'subject:Why': 0.09; 'python': 0.10; 'python.': 0.11; 'jan': 0.11; 'def': 0.13; 'lambda': 0.16; 'partly': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'reedy': 0.16; 'subject:default': 0.16; 'wrote:': 0.16; 'language': 0.19; 'first,': 0.20; 'this:': 0.23; 'header:In-Reply- To:1': 0.24; 'header:User-Agent:1': 0.26; "doesn't": 0.26; 'header:X-Complaints-To:1': 0.26; 'equivalent': 0.27; 'should': 0.36; 'needed': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'wanted': 0.37; 'end': 0.39; 'why': 0.39; 'does': 0.39; 'to:addr:python.org': 0.40; 'your': 0.60; 'received:96': 0.63; '>def': 0.84; 'folk': 0.91; 'received:fios.verizon.net': 0.91; 'why?': 0.91 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| X-Gmane-NNTP-Posting-Host | pool-96-227-207-81.phlapa.fios.verizon.net |
| User-Agent | Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.6.0 |
| In-Reply-To | <56F73B60.9020603@gmail.com> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.21 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Xref | csiph.com comp.lang.python:105856 |
Show key headers only | View raw
On 3/26/2016 9:46 PM, gvim wrote:
> Given that Python, like Ruby, is an object-oriented language why doesn't
> this:
>
> def m():
> a = []
> for i in range(3): a.append(lambda: i)
> return a
>
>def echo_i: return i
> b = m()
> for n in range(3): print(b[n]()) # => 2 2 2
) # => 2 2 2
>
> ... work the same as this in Ruby:
>
> def m
> a = []
> (0..2).each {|i| a << ->(){i}}
> a
> end
>
> aa = m
> (0..2).each {|n| puts aa[n].()} # => 0 1 2
Since Python came first, and Ruby was partly inspired by and derived
from Python, perhaps you should ask Ruby folk why it does not work the
same as Python. (Because Matz wanted it different.)
> lambda i=i: i
>
> ... is needed to make it work in Python. Just wondered why?
Your Python def m is essentially equivalent to
def m():
def echo_i(): return i
i = 2
return [echo_i]*3
--
Terry Jan Reedy
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Why lambda in loop requires default? Terry Reedy <tjreedy@udel.edu> - 2016-03-27 12:12 -0400
csiph-web