[Tutor] why do i get None as output

Andre Engels andreengels at gmail.com
Mon Sep 6 09:53:54 CEST 2010


On Mon, Sep 6, 2010 at 9:41 AM, Roelof Wobben <rwobben at hotmail.com> wrote:
>
>
>> To: tutor at python.org
>> From: alan.gauld at btinternet.com
>> Date: Mon, 6 Sep 2010 08:27:31 +0100
>> Subject: Re: [Tutor] why do i get None as output
>>
>>
>> "Roelof Wobben" <rwobben at hotmail.com> wrote
>>
>> def make_empty(seq):
>> word2=""
>> teller=0
>> if type(seq) == type([]):
>> teller=0
>> while teller < len(seq):
>> seq[teller]=""
>> teller = teller + 1
>> elif type(seq) == type(()):
>> tup2 = list (seq)
>> while teller > tup2.len():
>> tup2[teller]=""
>> teller = teller + 1
>> seq = tuple(tup2)
>> else:
>> seq = ""
>>
>> test = make_empty([1, 2, 3, 4])
>>
>> But now I get None as output instead of []
>>
>>
>> Because None is the default return value from a function.
>> If you do not return a value (which you don;t in this case) then
>> Python automatically returns None.
>>
>> You need to return something from your make_empty function.
>>
>> Also, if all you want to do is return an empty version of
>> whatever has been passed in there are much easier
>> ways of doing it! And in fact, a list of empty strings is
>> not the same as an empty list...
>>
>>
>> HTH
>>
>> --
>> Alan Gauld
>> Author of the Learn to Program web site
>> http://www.alan-g.me.uk/
>>
>>
>> _______________________________________________
>> Tutor maillist - Tutor at python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>
> Oke,
>
> I put a return seq in the programm and it looks now like this :
>
> def encapsulate(val, seq):
>     if type(seq) == type(""):
>         return str(val)
>     if type(seq) == type([]):
>         return [val]
>     return (val,)
>
> def insert_in_middle(val, seq):
>     middle = len(seq)/2
>     return seq[:middle] + encapsulate(val, seq) + seq[middle:]
>
> def make_empty(seq):
>     """
>       >>> make_empty([1, 2, 3, 4])
>       []
>       >>> make_empty(('a', 'b', 'c'))
>       ()
>       >>> make_empty("No, not me!")
>       ''
>     """
>     if type(seq) == type([]):
>         seq = []
>     elif type(seq) == type(()):
>         seq=()
>     else:
>         seq = ""
>     return seq
>
> if __name__ == "__main__":
>     import doctest
>     doctest.testmod()
>
> This works but I don't think its what the exercise means :
>
>
> Create a module named seqtools.py. Add the functions encapsulate and
> insert_in_middle from the chapter. Add doctests which test that these two
> functions work as intended with all three sequence types.
>
> Add each of the following functions to seqtools.py:
>
> def make_empty(seq):
>     """
>       >>> make_empty([1, 2, 3, 4])
>       []
>       >>> make_empty(('a', 'b', 'c'))
>       ()
>       >>> make_empty("No, not me!")
>       ''
>     """
>
> So i think I have to use encapsulate and insert_in_middle. And I don't use
> it.

I don't think so. They don't look like the kind of thing that would be
useful for this function. In your example seqtools.py is supposed to
be a (toy example of a) library, a collection of functions to do
things with sequence-like objects to be used by other programs. These
functions in general need not have much to do with eachother, except
that they work on the same type of objects.


-- 
André Engels, andreengels at gmail.com


More information about the Tutor mailing list