Try this instead (but see below for guessed better and more versatile version):
#! /usr/bin/env python
def ex_buggy_reverse(in_list):
m = in_list[:]
m.reverse()
return m
def test_ex_buggy_reverse():
assert [3, 2, 1] == ex_buggy_reverse([1, 2, 3])
assert [] == ex_buggy_reverse([])
a_list = ["a", "b", "c"]
assert ["c", "b", "a"] == ex_buggy_reverse(a_list)
assert ["a", "b", "c"] == a_list
test_ex_buggy_reverse()
On my python 2.7.11 it yields no output which is good. I changed some names just to change, I would not suggest to really name the function ex_buggy_reverse
;-)
You had IMO several issues in your code:
Do not use names for variables that are keywords (or builtins or often used functions in standard modules if possible) as this nearly always bites you when you expect it at least. input
is meant here.
reverse does not make copies, so why do it in the function. You might have better written as below this itemized list here.
Try to be PEP8 compliant (here the spaces following the comma in the list literal.
Sample of rewritten reverse_proxy
function (replacing buggy_reverse(...)
:
def reverse_proxy(an_iterable):
return reversed(an_iterable)
From the python documentation on reversed(): "Return a reverse iterator. seq must be an object which has a reversed() method or supports the sequence protocol (the len() method and the getitem() method with integer arguments starting at 0)."
So if you want to convert the iterable to a list, I'ld suggest to state that purpose in the name, and remember it looks clumsy to call the reverse method on a locally defined object (that reverses the elements of the list in place) and than return that object / reference instead of eg.
def as_reverse_list(an_iterable):
return list(reversed(an_iterable))
There is - with focus lists a solid question / anser page here: How can I reverse a list in python?.
Here the promised guessed enhanced and extended version:
#! /usr/bin/env python
def as_reverse_list(an_iterable):
return list(reversed(an_iterable))
def test_as_reverse_list():
assert [3, 2, 1] == as_reverse_list([1, 2, 3])
assert [] == as_reverse_list([])
a_list = ["a", "b", "c"]
assert ["c", "b", "a"] == as_reverse_list(a_list)
assert ["a", "b", "c"] == a_list
a_tuple = ("a", "b", "c")
assert ["c", "b", "a"] == as_reverse_list(a_tuple)
assert ["a", "b", "c"] == a_list
test_as_reverse_list()
HTH and happy hacking!