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


Groups > comp.lang.python > #78569 > unrolled thread

Re: Function passed as an argument returns none

Started byChris Kaynor <ckaynor@zindagigames.com>
First post2014-10-01 15:45 -0700
Last post2014-10-01 15:45 -0700
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Function passed as an argument returns none Chris Kaynor <ckaynor@zindagigames.com> - 2014-10-01 15:45 -0700

#78569 — Re: Function passed as an argument returns none

FromChris Kaynor <ckaynor@zindagigames.com>
Date2014-10-01 15:45 -0700
SubjectRe: Function passed as an argument returns none
Message-ID<mailman.14534.1412203584.18130.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

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):
>   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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web