1

I'm trying to pass a parameter to a function buggy_reverse.

def buggy_reverse(input):
    m = input
    m.reverse()
    return m


def test_buggy_reverse():
    assert [3,2,1] == buggy_reverse([1,2,3])
    assert [] == buggy_reverse([])
    input = ["a", "b", "c"]
    assert ["c", "b", "a"] == buggy_reverse(input)
    assert ["a", "b", "c"] == input

I've assigned input to m and reversed m . But in the process of reversing m input is also being reversed. Can somebody please explain why.

  • The line `m = input` makes `m` another name for your (list in this case) `input`. On my machine with python 2.7.11 and also 3.5.1 all assertions pass. Which version do you use it with? Well, of course, it does not get called ... Oh and I would avoid, naming variables with keywords (`input` in this case ...) Just imagine you want to input some thing from console, next time for checking, and try `my_in = input("Prompt>")` – Dilettant Jun 16 '16 at 06:38

1 Answers1

0

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:

  1. 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.

  2. reverse does not make copies, so why do it in the function. You might have better written as below this itemized list here.

  3. 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!

Community
  • 1
  • 1
Dilettant
  • 3,267
  • 3
  • 29
  • 29
  • Hm, a downvote is always just a click away, the why so hard to say ... one has to type and will leave an id ;-) – Dilettant Jun 16 '16 at 06:58