Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'newbie': 0.05; 'output': 0.05; 'element': 0.07; 'interpreter.': 0.07; 'subject:code': 0.07; 'subject:help': 0.08; 'arguments,': 0.09; 'executed': 0.09; 'happens.': 0.09; 'python': 0.11; '54,': 0.16; '55,': 0.16; '__future__': 0.16; 'argument,': 0.16; 'ball.': 0.16; 'behave': 0.16; 'default)': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'guarded': 0.16; 'loops': 0.16; 'python-list,': 0.16; 'sees': 0.16; 'separated': 0.16; 'splitting': 0.16; 'statement.': 0.16; 'subject:skip:u 10': 0.16; 'subject:python': 0.16; 'followed': 0.16; 'wrote:': 0.18; 'thu,': 0.19; 'import': 0.22; 'putting': 0.22; 'print': 0.22; '(by': 0.24; '31,': 0.24; 'merge': 0.24; 'this:': 0.26; 'asking': 0.27; 'header :In-Reply-To:1': 0.27; 'function': 0.29; 'rest': 0.29; 'list:': 0.30; 'subject:please': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'continues': 0.31; 'depth': 0.31; 'prints': 0.31; 'though.': 0.31; 'there.': 0.32; 'says': 0.33; 'subject:the': 0.34; 'something': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'thanks': 0.36; "i'll": 0.36; 'half': 0.37; 'list': 0.37; 'step': 0.37; 'to:addr:python-list': 0.38; 'issue': 0.38; 'list,': 0.38; 'pm,': 0.38; 'explain': 0.39; 'extremely': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'subject:Can': 0.60; 'back': 0.62; "you'll": 0.62; 'our': 0.64; 'more': 0.64; '30,': 0.65; 'here': 0.66; 'reply': 0.66; '20,': 0.68; '26,': 0.68; 'useful.': 0.68; 'secret': 0.74; 'soon,': 0.74; 'divide': 0.84; 'off,': 0.84; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=Y/cRQ8UmnB+yoJYaNjl3ZSjUg3R0W9kvovZO4L4oahw=; b=zQ7MT4ZxBM2BPmbb2I137obGdobxaulQAtzdr3AFRQd+yJ+ciZdvzXXOMfVVR/Fpv7 XTPoQPng93ekhGQFLPySDjIWPwhWCF3xkCD3Gcq+dFGMCqkm0Q/lrAPBxavWC7z3Qaea app3X7xr1mXcP5mUbwWiipZlXhotBkasHzq4QCCubJ2Bx9nYKFS+Fk5ic+acT+vz2bjZ ZC5MYYtgvUm7F9iEjojCew6CZcQVUtko4sF2HajJfvvG39U8+0xyArEaO8xZxHWcK6Zq NPmHtyxzR7MPEwxsD2wqHmRX0i/ogANsVCKq3yncStsgaFsXmV41NVSfpgNMRYOvnffQ +R3w== MIME-Version: 1.0 X-Received: by 10.220.167.69 with SMTP id p5mr4835154vcy.57.1369910842117; Thu, 30 May 2013 03:47:22 -0700 (PDT) In-Reply-To: References: <921173f3-21e7-46b4-a7b1-86660a3ebc72@googlegroups.com> Date: Thu, 30 May 2013 20:47:22 +1000 Subject: Re: Can anyone please help me in understanding the following python code From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 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: 54 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1369910850 news.xs4all.nl 15996 [2001:888:2000:d::a6]:51836 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:46452 On Thu, May 30, 2013 at 8:19 PM, wrote: > Thanks for the reply Chris. > > I am newbie to python, so please excuse me if I am asking chilly questions. All questions are welcome! > Can you please explain more about the following sentence. > "When it says "Splitting" with a single-element list, it then > immediately prints "Merging" and returns (because all the rest of the > code is guarded by the 'if'). Execution then continues where it left > off, in the parent." The code goes like this: 1) Print "Splitting" 2) If there's more than one element in the list: 2a) Divide the list in half 2b) Call self on each half 2c) Merge 3) Print "Merging In step 2b, all the steps from 1 through 3 are executed again (twice). Soon, those calls will just output "Splitting" followed by "Merging"; and then we go back to 2c. That's why it *seems* that the code goes from 3 to 2c. You'll notice that the call depth decreases when this happens. > Because I am not sure how the control can go back to top of the function unless there is no loops there. > > Also, Can you please let me know how did you found out that I am using Python 2 Interpreter. That one is actually based on my crystal ball. Here on python-list, we issue them to all our top respondents; they're extremely useful. I'll let you in on part of the secret of how this works, though. In Python 2, 'print' is (by default) a statement. When you give it something in parentheses, it sees a tuple: print("Splitting ",alist) ('Splitting ', [54, 26, 93, 17, 77, 31, 44, 55, 20]) In Python 3, 'print' is a function. It takes arguments, and prints out each argument, separated by spaces. print("Splitting ",alist) Splitting [17, 20, 26, 31, 44, 54, 55, 77, 93] You can make Python 2 behave the same way by putting this at the top of your program: from __future__ import print_function ChrisA