Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '*not*': 0.07; 'tries': 0.07; 'arguments': 0.09; 'calls.': 0.09; 'python': 0.11; 'def': 0.12; 'bug': 0.12; 'creates': 0.14; 'times,': 0.14; 'changes': 0.15; '9:20': 0.16; ':-(': 0.16; 'be:': 0.16; 'commas,': 0.16; 'concatenate': 0.16; 'distinct': 0.16; 'inheritance': 0.16; 'none.': 0.16; 'one-element': 0.16; 'subject:Java': 0.16; 'surprising': 0.16; 'tuple': 0.16; 'tuple,': 0.16; 'tuple.': 0.16; 'used:': 0.16; 'wrote:': 0.18; 'programming': 0.22; '(a)': 0.24; "aren't": 0.24; 'instead.': 0.24; 'java': 0.24; 'mon,': 0.24; 'second': 0.26; 'subject:/': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'correct': 0.29; 'resolution': 0.29; 'am,': 0.29; 'array': 0.29; 'raise': 0.29; 'related': 0.29; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; 'object.': 0.31; 'tuples': 0.31; 'class': 0.32; 'created': 0.35; 'something': 0.35; 'definition': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'next': 0.36; 'method': 0.36; 'list': 0.37; 'lists.': 0.38; 'to:addr:python-list': 0.38; 'list,': 0.38; 'previous': 0.38; 'expect': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'dangerous': 0.60; 'introduced': 0.61; "you're": 0.61; 'first': 0.61; 'back': 0.62; 'containing': 0.69; 'default': 0.69; '2015': 0.84; 'situations,': 0.84; 'subject:experience': 0.84; 'careful': 0.91; 'on?': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=Toa1JpfUPG+sqo0WsIKhIilc7ntPvJyul+c1X1nukFg=; b=w6N5h/5dZht50/09aNyct/kRcmyJzGHtnI/91HMLnXHgrouYXPbXkBxf3tUq0zqma4 anizIww46yjo2m/GS24rVvsORe3JAWs+XGIUpnVnbRWri/M2FTVpy9TtyYbEoHU9IIIj r+2rlo9m7pcDwXesWzZ5YgsfPkQ+SVvEEEn07ZCJrliZxm0My3HMDggehUM4nzzUw7ZI 6cgduM2RReRJzHYnYEf6vCXvvhSTXYtWPx1doLBmKjwumnipDMGoIf889641XADMmDCZ tozyGMlmW0MM3I4pPuRYWPdXcJStPSUGdVauY5FVcYW6M6PT5ILhRsbHAPMFhrAxqOyw qgaQ== X-Received: by 10.43.65.19 with SMTP id xk19mr3269682icb.20.1430761454172; Mon, 04 May 2015 10:44:14 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <87r3qwid3u.fsf@Equus.decebal.nl> References: <87r3qwid3u.fsf@Equus.decebal.nl> From: Ian Kelly Date: Mon, 4 May 2015 11:43:33 -0600 Subject: Re: Bitten by my C/Java experience To: Python Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ 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: 43 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1430761463 news.xs4all.nl 2935 [2001:888:2000:d::a6]:38332 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:89920 On Mon, May 4, 2015 at 9:20 AM, Cecil Westerhof wrote: > Potential dangerous bug introduced by programming in Python as if it > was C/Java. :-( > I used: > ++tries > that has to be: > tries += 1 > > Are there other things I have to be careful on? That does not work as > in C/Java, but is correct syntax. Some other gotchas that aren't necessarily related to C/Java but can be surprising nonetheless: * () is a zero-element tuple, and (a, b) is a two-element tuple, but (a) is not a one-element tuple. Tuples are created by commas, not parentheses, so use (a,) instead. * Default function arguments are created at definition time, not at call time. So if you do something like: def foo(a, b=[]): b.append(a) print(b) The b list will be the same list on each call and will retain all changes from previous calls. * super() doesn't do what you might expect in multiple inheritance situations, particularly if you're coming from Java where you never have to deal with multiple inheritance. It binds to the next class in the method resolution order, *not* necessarily the immediate superclass. This also means that the particular class bound to can vary depending on the specific class of the object. * [[None] * 8] * 8 doesn't create a 2-dimensional array of None. It creates one list containing None 8 times, and then it creates a second list containing the first list 8 times, *not* a list of 8 distinct lists. * If some_tuple is a tuple containing a list, then some_tuple[0] += ['foo'] will concatenate the list *but* will also raise a TypeError when it tries to reassign the list back to the tuple.