Which style is preferable?
pete at shinners.org
Fri Jul 16 17:01:22 CEST 2004
Antoon Pardon wrote:
> For the sake of this question I'm writing a vector class. So
> I have code something like the following
I find all the type checking a touch ugly, but perhaps I've been using
Python for too long. I would approach it like this.
def __init__(self, iterable):
self.val = map(float, iterable)
def __add__(self, term):
values = map(operator.add, self.val, term)
This will still give you a TypeError if non-float-compatible values are
passed. You will still get a TypeError if the add function gets an
object/Vector with odd types or the wrong size. One benefit, besides
being ridiculously simple, is it accepts integers as arguments, and you
can do operations with vectors against lists and tuples with the same
number of elements.
myvec += 1, 0, 1
More information about the Python-list