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


Groups > comp.lang.python > #46452

Re: Can anyone please help me in understanding the following python code

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 <rosuav@gmail.com>
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 <fd037baa-b5b0-435e-b035-4e25b0d3acbd@googlegroups.com>
References <921173f3-21e7-46b4-a7b1-86660a3ebc72@googlegroups.com> <fd037baa-b5b0-435e-b035-4e25b0d3acbd@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 <rosuav@gmail.com>
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 <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.2402.1369910850.3114.python-list@python.org> (permalink)
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

Show key headers only | View raw


On Thu, May 30, 2013 at 8:19 PM,  <bhk755@gmail.com> 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

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


Thread

Can anyone please help me in understanding the following python code bhk755@gmail.com - 2013-05-30 02:48 -0700
  Re: Can anyone please help me in understanding the following python code Chris Angelico <rosuav@gmail.com> - 2013-05-30 20:01 +1000
  Re: Can anyone please help me in understanding the following python code Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2013-05-30 10:17 +0000
  Re: Can anyone please help me in understanding the following python code bhk755@gmail.com - 2013-05-30 03:19 -0700
    Re: Can anyone please help me in understanding the following python	code Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2013-05-30 10:43 +0000
    Re: Can anyone please help me in understanding the following python code Chris Angelico <rosuav@gmail.com> - 2013-05-30 20:47 +1000
    Re: Can anyone please help me in understanding the following python code Joshua Landau <joshua.landau.ws@gmail.com> - 2013-05-30 12:11 +0100
  Re: Can anyone please help me in understanding the following python code Joshua Landau <joshua.landau.ws@gmail.com> - 2013-05-30 12:03 +0100
  Re: Can anyone please help me in understanding the following python code bhk755@gmail.com - 2013-05-30 05:39 -0700
    Re: Can anyone please help me in understanding the following python code bhk755@gmail.com - 2013-05-30 05:42 -0700
      Re: Can anyone please help me in understanding the following python code Dave Angel <davea@davea.name> - 2013-05-30 09:29 -0400
    Re: Can anyone please help me in understanding the following python code bhk755@gmail.com - 2013-05-30 05:42 -0700
    Re: Can anyone please help me in understanding the following python code Chris Angelico <rosuav@gmail.com> - 2013-05-30 22:55 +1000
  Re: Can anyone please help me in understanding the following python code bhk755@gmail.com - 2013-05-30 06:01 -0700
  Re: Can anyone please help me in understanding the following python code bhk755@gmail.com - 2013-05-30 21:53 -0700
  Re: Can anyone please help me in understanding the following python code bhk755@gmail.com - 2013-05-30 21:54 -0700
    Re: Can anyone please help me in understanding the following python code Cameron Simpson <cs@zip.com.au> - 2013-05-31 15:43 +1000
    Re: Can anyone please help me in understanding the following python code Chris Angelico <rosuav@gmail.com> - 2013-05-31 17:13 +1000
  Re: Can anyone please help me in understanding the following python code rusi <rustompmody@gmail.com> - 2013-06-01 10:43 -0700

csiph-web