Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'broken': 0.03; 'value,': 0.03; 'argument': 0.04; 'true,': 0.04; 'subject:Python': 0.05; 'context': 0.05; 'elements.': 0.05; 'guido': 0.05; 'none,': 0.05; 'advocate': 0.07; 'assign': 0.07; 'caller': 0.07; 'default.': 0.07; 'expressions': 0.07; 'function,': 0.07; 'interpreted': 0.07; 'variables.': 0.07; 'python': 0.09; '"if': 0.09; '2.3,': 0.09; '22,': 0.09; 'alter': 0.09; 'ambiguity': 0.09; 'behavior,': 0.09; 'bool': 0.09; 'decision.': 0.09; 'declarations': 0.09; 'difference,': 0.09; 'expected.': 0.09; 'immutable': 0.09; 'mutable': 0.09; 'notation': 0.09; 'objects.': 0.09; 'python:': 0.09; 'semantics': 0.09; 'tends': 0.09; 'thereof.': 0.09; 'type;': 0.09; 'def': 0.10; 'language,': 0.11; 'index': 0.13; 'language': 0.14; 'java,': 0.15; '"def"': 0.16; '"elif"': 0.16; '"long': 0.16; '"some': 0.16; '(also': 0.16; '(meaning': 0.16; '42;': 0.16; 'boolean': 0.16; 'bugs.': 0.16; 'comparison.': 0.16; 'conditional': 0.16; 'cryptic': 0.16; 'evaluates': 0.16; 'example)': 0.16; 'foo(int': 0.16; 'form"': 0.16; 'merely': 0.16; 'personally,': 0.16; 'quite.': 0.16; 'semantically': 0.16; 'sequence,': 0.16; 'sharp': 0.16; 'statement.': 0.16; 'statements,': 0.16; 'value;': 0.16; 'verbose': 0.16; 'zero,': 0.16; 'later': 0.16; 'wrote:': 0.17; 'basically': 0.17; 'copied': 0.17; 'variables': 0.17; 'examples': 0.18; 'feb': 0.19; '(not': 0.20; 'appropriate': 0.20; 'code.': 0.20; 'variable': 0.20; 'changes': 0.20; 'equivalent': 0.20; 'mostly': 0.20; 'otherwise,': 0.20; 'parameters': 0.20; 'assignment': 0.22; 'context.': 0.22; 'finally,': 0.22; 'implicit': 0.22; 'latter': 0.22; 'modifying': 0.22; 'visible': 0.22; 'void': 0.22; 'originally': 0.23; "python's": 0.23; 'statement': 0.23; 'seems': 0.23; 'idea': 0.24; 'external': 0.24; 'specifically': 0.24; 'least': 0.25; 'header:In-Reply-To:1': 0.25; 'creating': 0.26; 'values': 0.26; 'checking': 0.27; '(as': 0.27; 'c++': 0.27; 'have,': 0.27; 'instead.': 0.27; 'is?': 0.27; 'object,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'arrays': 0.29; 'comparison': 0.29; 'context,': 0.29; 'implicitly': 0.29; 'index,': 0.29; 'mind,': 0.29; 'overhead': 0.29; 'types.': 0.29; 'way?': 0.29; 'objects': 0.29; 'this.': 0.29; 'that.': 0.30; 'fri,': 0.30; 'keyword': 0.30; 'function': 0.30; 'point': 0.31; 'generally': 0.32; 'good.': 0.32; 'cases,': 0.33; 'function.': 0.33; 'int': 0.33; 'anyone': 0.33; 'to:addr:python-list': 0.33; 'languages': 0.33; 'version': 0.34; 'received:google.com': 0.34; 'learned': 0.65; 'forward': 0.66; 'today': 0.67; 'protect': 0.69; 'benefit': 0.70; 'reviewed': 0.74; '2013': 0.84; 'designed,': 0.84; 'to:name:python': 0.84; 'not:': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:mime-version:in-reply-to:references:from:date:message-id :subject:to:content-type:content-transfer-encoding; bh=FCs/DAgSmA2oJGYDPWWCbN7bpsx5ePVAlVOVonNDwVM=; b=cOqVFD8zdqmDkMBP6DeDwXd7DfVHICdhtr00DqPwBlbh/q4SCejyLPZhCTzRYbmAxj zh9AYPCFqkaGP17I31mWnYh5WAcqhwVBLaFQ5dYTOrw8t8OITcmE1rDYJmhTDUMqyzkF LnE1wiofMBW259CBR1SdIf8b0iViCG2ULPQq94b0R8HC6xMy8n/7T6mLOERtUQENfhZR J1Ud/VFdGc18IjZgjysqceLOnKoEuPOr9up9Et8kuuGtnTQntqKirte3GAvJAGHPI/q+ c+h+mkjbt1ynLJyfbZqh0I7UgsMfhBE5AGZS7cpz0JDwkhJlCgla0jwSfy8XxTZJpI4J bj8w== X-Received: by 10.220.239.71 with SMTP id kv7mr4953831vcb.46.1361573176270; Fri, 22 Feb 2013 14:46:16 -0800 (PST) MIME-Version: 1.0 In-Reply-To: <928d2cf7-728b-4f35-b8c9-4c9b958507e5@googlegroups.com> References: <5127848B.1060004@gmail.com> <928d2cf7-728b-4f35-b8c9-4c9b958507e5@googlegroups.com> From: Ian Kelly Date: Fri, 22 Feb 2013 15:45:35 -0700 Subject: Re: Python Newbie To: Python Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 141 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1361573184 news.xs4all.nl 6874 [2001:888:2000:d::a6]:44124 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:39621 On Fri, Feb 22, 2013 at 2:37 PM, wrote: > There seems to be a "heated" argument about Python's apparently intention= al ambiguity in conditional statements. Specifically, the issue is, is it m= ore appropriate to write (as an example) > > if (some statement): # short form > > rather than > > if (some statement =3D=3D true): # long form Nobody would advocate that particular comparison. If the result of "some statement" is already a boolean value, then just use that expression in your if statement. Checking "if True =3D=3D True:" is a pointlessly verbose comparison when you can just check "if True:". The tension here is not between "short form" and "long form" if statements, but between using conditional expressions that evaluate to booleans versus using conditional expressions that are implicitly interpreted as booleans. Some people prefer only the former. Others see value in the latter for some (not all!) conditions. The usual advice given for implicit interpretation is that it is a test for "something" versus "nothing". If I do "if x:" and it evaluates as true, then I know there is something in x. Otherwise, I have nothing: None, or zero, or an empty sequence, etc. Generally the context will give you more information about what types are expected. Personally, I find that this tends to be most useful as a test for whether a sequence contains elements. > Some 50(?) years ago, C was designed so that everything other than 0 eval= uated to true and was false otherwise. Fast forward to recent memory, when = C# was designed, Microsoft claims they reviewed all the features of C, C++ = and Java, pulled the best features from each of these languages and designe= d a new language that would help minimize the potential for planting bugs. = Say what you want about MS inventions, but my experience is that to require= the long form notation was a good decision. For me the fact that the short= notation is legal in Python is a stepback in language design. Python inven= tors, when creating what is after all considered a contemporary language, s= hould have known better. Call me psychopath if you will (have seen this in = one post), but I shall continue to use the aforementioned long form as I al= ways have, and no Python is going to change that. At least one part of the reason that Python does not specifically require booleans in conditions is that the language did not originally have a boolean type; 1 and 0 were generally used instead. The bool type was not added until version 2.3, and requiring booleans in conditions at that point would have broken a large amount of code. > Today I learned the hard way that all function parameters in Python are p= assed by reference (meaning whatever happens to them inside a function, new= values are always passed to caller). Not quite. Python uses pass-by-object (also known as "pass-by-sharing") semantics. The argument seen by the function is the same object that was passed to it. The name bound to the object inside the function is unrelated to the name bound to the object in the caller's context, so that if you assign something else to the name, it will not affect the object or its binding to the external name. Since it's the same object, though, you can mutate the object and then later observe those changes in the external context. Note that this is only relevant for mutable data types. For immutable types such as strings and ints, you cannot alter the passed-in object, so it is semantically equivalent to pass-by-value without the overhead of actually copying anything. > Not good. I got caught up on this. To combat the mostly unwanted behavior= , inside a function I have to reassign variables intended to be local to ne= w variables. Reassigning the objects to new variables would make no difference, as you would still be dealing with the same objects. You would have to actually copy the objects in order that changes to them would only be local to the function. You would then be free to assign the copied objects back to the original variable name, and then you would no longer have any reference to the original objects within the function. In other words, this will protect a list passed in from being mutated: def foo(my_list): my_list =3D list(my_list) ... This will not: def foo(my_list): another_name_for_my_list =3D my_list ... > A pain. Can anyone offer ONE reason why Python was designed that way? How about because the overhead of copying every object passed into every function would be inefficient? What Python does here is not all that different from how .NET reference types are passed by default. The call semantics of these two examples are basically the same: # Python: def foo(index, my_list): my_list[index] =3D 42 /* C Sharp */ void foo(int index, int[] my_list) { my_list[index] =3D 42; } In both cases, reassigning index would have no effect outside the function. In C#, it's because the argument is passed by value; in Python, it's because the assignment merely binds a different object to the name. In both cases, reassigning my_list would have no effect outside the function, for the same reasons. In both cases, modifying index without reassigning it would still have no effect outside the function. In C#, this is because int is a value type; in Python, it's because ints are immutable in the first place. Finally, in both cases, modifying the contents of my_list *does* have a visible effect outside the function. In C#, it's because arrays are a reference type; in Python, it's because the list object is shared by the caller and callee. So with that in mind, I have a hard time understanding why you might be finding this strange and unexpected. > Out of curiosity, does anyone have any idea why function declarations are= preceded by the keyword "def" rather than something more intuitive like "f= unction" or at least "func", perhaps? Because that's what Guido picked when he started writing the language in 1989, and it's too late to change it. > Does anyone know what the benefit of writing the cryptic "elif" to mean "= else if" is? Curiously, the default statement in an if/else chain is preced= ed by "else" and not "el". Lots of languages use "elif" or "elsif" or "elseif" or some variation thereof. Why does it matter?