Function passed as an argument returns none

Chris Angelico rosuav at
Thu Oct 2 10:54:47 CEST 2014

On Thu, Oct 2, 2014 at 6:46 PM, Shiva
<shivaji_tn at> wrote:
> Hi All,
> Thank you everyone. This is fantastic - I post a query and go to sleep and
> by the time I get up there is already a nice little thread of discussion
> going on.....

Yeah, that's what python-list is like! Busy list, lots of opinionated
people here...

> By the way, I sorted it with all your suggestions.
> def donuts(count):
>   if count <= 9:     #This had to be 9 instead of 5 as per the question req.
>     return 'Number of donuts: {0}'.format(count)
>   else:
>     return 'Number of donuts: many'

Looks good!

> So to summarise what I learnt:
> * Just 'return' returns None - it is not related to what you print inside
> the function.If you want something specific out of a function return
> something specific.

Correct. As Steven and I said, printing is unrelated to returned
values. Running off the end of a function, or using "return" on its
own, is the same as "return None".

> * return 'Number of donuts: ',count    returns a tuple like:
> ('Number of donuts: ',9)

Right. The comma creates a tuple; the parentheses are printed on
display, to make it easier to read, but it's the comma that does it.
The comma has different meaning in some places, such as a function
call (like print), but otherwise it'll create a tuple.

> * To just print the string without returning it as tuple , use string
> formatting.

Yep! You're three for three. There are other ways to do things (you
could use str() and concatenation, or percent formatting, or any
number of ways), but they all do the same thing: they construct a
single string, which you can then return. Incidentally, when you use
.format() as you do there, the number 0 can be omitted - it's obvious
to the parser, so you can shortcut it:

>>> "Hello, {}!".format("world")
'Hello, world!'


More information about the Python-list mailing list