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: 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 To: Wilfried Falk Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: "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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On 18 January 2012 09:52, Wilfried Falk wrote: > Hello Pythons, > > attached to this email is a pdf-file which shows, that=C2=A0 "+=3D" does = not work > well all along. Mybe somebody of you is able to explain my observations i= n > 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 +=3D 1 you sometimes get print(); identifier =3D 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 +=3D 1 with identifier =3D 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 +=3D [something] is not the same as _list =3D _list + [something]. def conc1(a, _list =3D []): _list =3D _list + [a] return _list def conc2(a, _list =3D []): _list +=3D [a] return _list # Main Program for i in range(4): _list =3D conc1(i) print(_list) print() for i in range(4): _list =3D 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 +=3D [a] and lst =3D 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 --=20 Arnaud