Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'value,': 0.04; 'interpreter': 0.05; 'debugging.': 0.09; 'formatting': 0.09; 'parameter': 0.09; 'prefix': 0.09; 'subject:Function': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; '"%s"': 0.16; 'execute,': 0.16; 'expected)': 0.16; 'expected):': 0.16; 'interpreter,': 0.16; 'loop.': 0.16; 'merely': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'code,': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'instead.': 0.24; 'string,': 0.24; 'test.': 0.24; 'fine': 0.24; 'looks': 0.24; 'cc:2**0': 0.24; 'read,': 0.26; 'defined': 0.27; 'values': 0.27; 'gets': 0.27; 'header:In-Reply- To:1': 0.27; 'idea': 0.28; 'function': 0.29; 'rest': 0.29; 'chris': 0.29; 'message-id:@mail.gmail.com': 0.30; 'getting': 0.31; 'prints': 0.31; 'probably': 0.32; 'run': 0.32; 'quite': 0.32; 'alone': 0.33; 'actual': 0.34; 'received:74.125.82': 0.34; 'could': 0.34; 'display': 0.35; 'test': 0.35; 'received:google.com': 0.35; 'returning': 0.36; 'hi,': 0.36; 'expected': 0.38; 'skip:& 10': 0.38; 'pm,': 0.38; 'rather': 0.38; 'that,': 0.38; 'received:74.125': 0.39; 'does': 0.39; 'called': 0.40; 'even': 0.60; 'above,': 0.60; 'mentioned': 0.61; 'below:': 0.68; 'fact,': 0.69; 'below.': 0.71; 'alone.': 0.84; 'returns.': 0.84 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-type; bh=Lk3oyArb1Lpc6YGRPNN3ogMYjXycBTnKnnj+C9BtxSI=; b=Ynco/I5KcTe7hGVyxdP2oIWRVEn6Vb6TxryxDN0Wpq0+nNCKvGRvwL3/A8+o9CihDw N3APqWYNtl/wCzwwwGF7Ftdj7ioEHQg38VG+NxigiIuBy2tHqgBvME+Fw9JlHZ+6805Z l2VF+2FOS6imDnmcAXKTcOxDZDHlq1Oap783mbNSQmn4IJMWehrJBXmEClWtEk+cgJ42 yOrF5GP0yy4v/wQ7qHmgilcGnzaHCanF7zVjn5JjZEIA4uu6JYnygF/8aBw+yGILEU9Z nEGbuDQm3OtgptRSkmV9IEq2IdlcC6Gq626eh1jjNy0xlVFd/ztsUlA7dxM6I/xfRwhz JBvA== X-Gm-Message-State: ALoCoQkPNVx6vEAZMrEgL13SDBxLU47SqW6nxmwAF2d9JOqXh1ZkX6gvQZFCYYJy/L2XkkMeK5YS X-Received: by 10.180.96.71 with SMTP id dq7mr213027wib.66.1412203576441; Wed, 01 Oct 2014 15:46:16 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: From: Chris Kaynor Date: Wed, 1 Oct 2014 15:45:56 -0700 Subject: Re: Function passed as an argument returns none To: Shiva Content-Type: multipart/alternative; boundary=f46d04448015865cc1050464440b Cc: "python-list@python.org" 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: 152 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1412203584 news.xs4all.nl 2839 [2001:888:2000:d::a6]:49631 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:78569 --f46d04448015865cc1050464440b Content-Type: text/plain; charset=UTF-8 Chris On Wed, Oct 1, 2014 at 3:37 PM, Shiva wrote: > Hi, > I am learning Python (version 3.4) strings.I have a function that takes in > a > parameter and prints it out as given below. > > def donuts(count): > if count <= 5: > print('Number of donuts: ',count) > else: > print('Number of donuts: many') > return > Print actually prints the value, and does not return it. Based off the rest of your code, you probably want this to look like (untested): def donuts(count): if count <= 5: return 'Number of donuts: {}'.format(count) # You could also use other formatting methods here, if you prefer the printf-style "%s" instead. else: return 'Number of donuts: many' Which will cause donuts to return the actual string, rather than printing it out to the TTY. Note that this means that, if run from the interpreter as you have been trying, the interpreter will now display "Number of donuts: 4" - including the quotes. > It works fine if I call > donuts(5) > > It returns: > we have 5 DN (as expected) > > However if I do : > > test(donuts(4), 'Number of donuts: 4') > If you do as mentioned above, donuts will then return the value, and it will be passed into test. > where test is defined as below: > > def test(got, expected): > print('got: ', got, 'Expected:' ,expected) > if got == expected: > prefix = ' OK ' > else: > prefix = ' X ' > print (('%s got: %s expected: %s') % (prefix, repr(got), repr(expected))) > > > Only 'None' gets passed on to parameter 'got' instead of the expected value > of 4. > Any idea why 'None' is getting passed even though calling the donuts(4) > alone returns the expected value? > donuts is, in fact, returning None when called alone. It merely looks like it is returning it as, when run from the interpreter, Python prints the "repr" of the return values of any non-None returns. This is known as a REPL: read, execute, print, loop. It is quite handy for debugging. Chris --f46d04448015865cc1050464440b Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Chris

On Wed, Oct 1, 2014 at 3:37 PM, Shiva <shivaji_tn@yahoo.com.dmarc.invalid> wrote:
Hi,
I am learning Python (version 3.4) strings.I have a function that takes in = a
parameter and prints it out as given below.

def donuts(count):
=C2=A0 if count <=3D 5:
=C2=A0 =C2=A0 print('Number of donuts: ',count)
=C2=A0 else:
=C2=A0 =C2=A0 print('Number of donuts: many')
=C2=A0 =C2=A0 return

Print actually pri= nts the value, and does not return it.

Based off t= he rest of your code, you probably want this to look like (untested):
=

def donuts(count):
=C2=A0 if count <=3D 5:
=C2= =A0 =C2=A0 return 'Number of donuts: {}'.format(count) # You could = also use other formatting methods here, if you prefer the printf-style &quo= t;%s" instead.
=C2=A0 else:
=C2=A0 =C2=A0 return 'Number of = donuts: many'

Which will cause donuts to retur= n the actual string, rather than printing it out to the TTY.

=
Note that this means that, if run from the interpreter as you ha= ve been trying, the interpreter will now display "Number of donuts: 4&= quot; - including the quotes.
=C2=A0
It works fine if I call
donuts(5)

It returns:
we have 5 DN=C2=A0 (as expected)

However if I do :

test(donuts(4), 'Number of donuts: 4')

If you do as mentioned above, donuts will then return the value, an= d it will be passed into test.
=C2=A0
where test is defined as below:

def test(got, expected):
=C2=A0 print('got: ', got, 'Expected:' ,expected)
=C2=A0 if got =3D=3D expected:
=C2=A0 =C2=A0 prefix =3D ' OK '
=C2=A0 else:
=C2=A0 =C2=A0 prefix =3D '=C2=A0 X '
=C2=A0 print (('%s got: %s expected: %s') % (prefix, repr(got), rep= r(expected)))


Only 'None' gets passed on to parameter 'got' instead of th= e expected value
of 4.
Any idea why 'None' is getting passed even though calling the donut= s(4)
alone returns the expected value?

donut= s is, in fact, returning None when called alone. It merely looks like it is= returning it as, when run from the interpreter, Python prints the "re= pr" of the return values of any non-None returns. This is known as a R= EPL: read, execute, print, loop. It is quite handy for debugging.

Chris

--f46d04448015865cc1050464440b--