Closed norases closed 10 years ago
might want one more test, o/w looks good!
Yeah, let's add a test for add :)
You rock! :rocket: Ship it!
2 potential things:
instance + instance
and instance + noninstance
. If you want to support noninstance + instance
, you need __radd__
which can be implemented in terms of add: https://www.inkling.com/read/learning-python-mark-lutz-4th/chapter-29/right-side-and-in-place__iadd__
. iadd is for +=. add and iadd will be very similar; just iadd will return self. It works right now b/c it's turning "x += y" into "x = x + y" which may be surprising to users who will expect += on mutable objects to operate in-place.im still checking around to see what's idiomatic
Updates after talking to @Feriority:
__radd__ = __add__
since addition is commutative.implement `iadd``. a testcase from @Feriority indicating why the current behavior would be shocking:
>>> def add_lists(x, y):
... x+=y
...
>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> add_lists(a, b)
>>> a
[1, 2, 3, 4, 5, 6]
>>>
Without a iadd function, the last print of a
would give its initial value.
adding @Feriority as a reviewer; his input has been very handy in helping me understand this python stuff :)
lastly, I'm not 100% sure what the easiest way to test iadd is. I think checking the object id. Like:
x = ...
y = ...
x_old_ix = id(x)
x += y
x_new_id = id(x)
assert_equal(x_old_id, x_new_id)
ids would only match if x is not a new object (which only happens if iadd is implemented). I think this is robust & safe
couple of comment changes and one additional thing to check in testing o/w looks good to me
Mostly looks good. Only comment is the docstring lines are pretty long, and should probably be wrapped to multiple lines. Fix & ship.
I fixed a comment line that is really long. the rest @sc932 said it is ok. Also broke up the assertion in last test
***** PEOPLE ***** Primary reviewer: @sc932
Reviewers: @suntzu86 @Feriority
***** DESCRIPTION ** Branch Name: norases_387_fix_bandit_bugs_and_add_new_tests Ticket(s)/Issue(s): Closes #387
***** TESTING DONE ***** make test make style-test