[Tutor] why do i get None as output
Hello, I have this programm: 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!) '' 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]) print test But now I get None as output instead of [] Can anyone explain why that happens ? Roelof ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] why do i get None as output
On Mon, Sep 6, 2010 at 8:34 AM, Roelof Wobben rwob...@hotmail.com wrote: Hello, I have this programm: 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!) '' 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]) print test But now I get None as output instead of [] Can anyone explain why that happens ? test = make_empty([1, 2, 3, 4]) makes test equal to the return value of make_empty. But make_empty does not return anything, and in that case its return value is made equal to empty. Compare: def f(x): x = x + 1 def g(x): x = x + 1 return x def h(x): return x +1 print f(1) None print g(1) 2 print h(1) 2 -- André Engels, andreeng...@gmail.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] why do i get None as output
Roelof Wobben rwob...@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@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] why do i get None as output
To: tutor@python.org From: alan.ga...@btinternet.com Date: Mon, 6 Sep 2010 08:27:31 +0100 Subject: Re: [Tutor] why do i get None as output Roelof Wobben rwob...@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@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. Roelof ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] why do i get None as output
On Mon, Sep 6, 2010 at 9:41 AM, Roelof Wobben rwob...@hotmail.com wrote: To: tutor@python.org From: alan.ga...@btinternet.com Date: Mon, 6 Sep 2010 08:27:31 +0100 Subject: Re: [Tutor] why do i get None as output Roelof Wobben rwob...@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@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, andreeng...@gmail.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] why do i get None as output
On 06/09/2010 8.34, Roelof Wobben wrote: Hello, I have this programm: ... def make_empty(seq): make_empty([1, 2, 3, 4]) [] make_empty(('a', 'b', 'c')) () make_empty(No, not me!) '' 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]) print test But now I get None as output instead of [] I would add a line like: return seq at the end of the make_empty function. Can anyone explain why that happens ? I think Python doesn't know what exactly is the value you need to receive from the make_empty function. That's why I'd make it clear. Otherwise, you know what happens... I quote the following from http://diveintopython.org/getting_to_know_python/declaring_functions.html In fact, every Python function returns a value; if the function ever executes a return statement, it will return that value, otherwise it will return None, the Python null value. Roelof Francesco Nessun virus nel messaggio in uscita. Controllato da AVG - www.avg.com Versione: 9.0.851 / Database dei virus: 271.1.1/3115 - Data di rilascio: 09/05/10 08:34:00 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor