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


Groups > comp.lang.python > #19094

Re: "+=" does not work correct all alogn

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <arnodel@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'python.': 0.04; '"""': 0.07; 'does.': 0.07; 'python': 0.08; '1.)': 0.09; '[0,': 0.09; '[0]': 0.09; 'bug,': 0.09; 'mutable': 0.09; 'def': 0.13; 'cc:addr :python-list': 0.15; '"+="': 0.16; "'a'": 0.16; '2.)': 0.16; 'beginners.': 0.16; 'do!': 0.16; 'explanation': 0.16; 'identifier': 0.16; 'observations': 0.16; 'url:effbot': 0.16; 'url:zone': 0.16; 'wrote:': 0.16; 'arguments': 0.18; 'subject:not': 0.21; 'header:In-Reply-To:1': 0.22; 'along.': 0.23; 'end,': 0.23; 'cc:2**0': 0.25; 'code': 0.25; 'code.': 0.26; 'function': 0.27; 'received:209.85.220': 0.27; 'somebody': 0.27; 'message-id:@mail.gmail.com': 0.28; 'cc:addr:python.org': 0.29; 'second': 0.29; 'quite': 0.31; '---': 0.31; 'attach': 0.32; 'does': 0.32; 'list': 0.32; "can't": 0.32; 'named': 0.33; 'object': 0.33; 'there': 0.33; 'answers': 0.34; 'creates': 0.34; 'rule': 0.34; 'rather': 0.34; 'end.': 0.34; 'comment': 0.35; 'post': 0.36; 'two': 0.37; 'but': 0.37; "there's": 0.37; 'received:google.com': 0.37; 'another': 0.37; 'happens': 0.37; 'could': 0.37; 'received:209.85': 0.38; 'think': 0.38; 'sometimes': 0.38; 'url:org': 0.39; 'received:209': 0.39; 'difference': 0.40; 'more': 0.61; 'body': 0.61; 'your': 0.61; 'below.': 0.63; 'here:': 0.67; 'respect.': 0.67; 'glad': 0.68; 'url:htm': 0.72; '[3]': 0.73; 'falk': 0.84; 'malfunction': 0.84; 'print()': 0.84; 'shows,': 0.84; 'things:': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=9LcZaWT4BxmeoxIayQ2IxYyVc0dqJ+V0Fr4/+1dzQek=; b=SN0V/DOZnlOGfS/sp6ZrNy2hNgAy6b8Hyd1ISxPz+5fGeOO5uaqEuCLxjcz94ztX8Q B6DFhkkyDgEs55H+eWzM5GFsxC1TUFyfxaTaOYMzOBPBQCe2071dO5r1mky7pQDyKtBG IJagsTR3UMsdIpeEjyB15E+rWQFyzQEF5vS4I=
MIME-Version 1.0
In-Reply-To <1326880375.27881.YahooMailNeo@web29007.mail.ird.yahoo.com>
References <1326880375.27881.YahooMailNeo@web29007.mail.ird.yahoo.com>
Date Wed, 18 Jan 2012 17:09:55 +0000
Subject Re: "+=" does not work correct all alogn
From Arnaud Delobelle <arnodel@gmail.com>
To Wilfried Falk <w_h_falk@yahoo.de>
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
Cc "python-list@python.org" <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4843.1326906598.27778.python-list@python.org> (permalink)
Lines 80
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1326906598 news.xs4all.nl 6857 [2001:888:2000:d::a6]:35154
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:19094

Show key headers only | View raw


On 18 January 2012 09:52, Wilfried Falk <w_h_falk@yahoo.de> wrote:
> Hello Pythons,
>
> attached to this email is a pdf-file which shows, that  "+=" does not work
> well all along. Mybe somebody of you is able to explain my observations in
> this respect. I will be glad about an answer.

I think you are more likely to get answers if you post your code in
the body of your message rather than attach it in a pdf, which is
quite an unusual thing to do!

"""
1.) For   identifier += 1   you sometimes get    print(); identifier =
identifier + 1
What means that there is a prior  print().  I could not find out a
rule for when this happens --- but it
does. By replaceing  identifier += 1   with   identifier = identifier
+ 1    the malfunction (print()) allways
disappears
"""

I can't comment on this as you don't provide any code.

"""
2.)  Another  "mystery"  is shown by the code below. There   _list +=
[something]    is not the same
as   _list = _list + [something].
def conc1(a, _list = []):
    _list = _list + [a]
    return _list
def conc2(a, _list = []):
    _list += [a]
    return _list
# Main Program
for i in range(4):
    _list = conc1(i)
    print(_list)
print()
for i in range(4):
    _list = conc2(i)
    print(_list)
In the first case the result of   print(_list)   is:
[0]
[1]
[2]
[3]
In the second case the result of   print(_list)   is:
[0]
[0, 1]
[0, 1, 2]
[0, 1, 2, 3]
"""

This behaviour is not a bug, it is a consequence of two things:

1. The way mutable default arguments work in Python.  Your
misunderstanding is a very common one for Python beginners.  There's a
good explanation of the behaviour here:

    http://effbot.org/zone/default-values.htm

2. The difference between

    lst += [a]

and

    lst = lst + [a]

The first one mutates the list object named 'lst' by appending 'a' at
its end, whereas the second one creates a new list made of the items
of lst with 'a' appended at the end.  So your function conc1 does not
mutate _list, whereas your function conc2 does.

HTH

-- 
Arnaud

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


Thread

Re: "+=" does not work correct all alogn Arnaud Delobelle <arnodel@gmail.com> - 2012-01-18 17:09 +0000
  Re: "+=" does not work correct all alogn woooee <woooee@gmail.com> - 2012-01-18 09:42 -0800

csiph-web